Hello. Tell me, please, I plotted a wav sound signal in the time domain (which is presented below), but instead of seconds, samples are displayed, how can you count the samples as seconds?

enter image description here

I wrote a code that allows you to get the sampling rate from a wav file, and then I don’t understand how to translate samples in seconds

`// Метод, получающий частоту дискретизации public void GetSampleRate(string waveFile) { //Читаем данные using (var fs = new FileStream(waveFile, FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { RiffId = br.ReadBytes(4); Size = br.ReadUInt32(); WavId = br.ReadBytes(4); FmtId = br.ReadBytes(4); FmtSize = br.ReadUInt32(); Format = br.ReadUInt16(); Channels = br.ReadUInt16(); SampleRate = br.ReadUInt32(); BytePerSec = br.ReadUInt32(); BlockSize = br.ReadUInt16(); Bit = br.ReadUInt16(); DataId = br.ReadBytes(4); DataSize = br.ReadUInt32(); // Читаем канал ReadChannel(br); } } } ` 

1 answer 1

The SampleRate variable is the number of samples per second. Accordingly, the time for the sample sample_number will be:

 var time = sample_number / SampleRate; 

Here time will be integer. If you need a split second:

 var time = 1f * sample_number / SampleRate; 
  • And sample_count is what I understand the reference number is divided by the sampling rate? - Andrey273
  • one
    No, sample_count is the reference number. Only I called him badly, better sample_number . - Uladzimir Palekh
  • Tell me, please, and you can do it? I read the data from the wav file and write it into the data array, then you can make sure that a certain amplitude corresponds to a specific second, I decided to write like this: for(int n = 0; n < data.Length; n++) {var time = n / SampleRate; } - Andrey273
  • n in this case, I decided to present it as the reference number, the data array, that is, the array index is the reference number, and the index value is the amplitude and then I would like to present time as a variable time on the x - axis graph - Andrey273
  • I did not quite understand the question, to be honest. The amplitude varies with the sampling rate, if you just need time in seconds to display on the graph, then yes, var time = n / SampleRate; , but you need to take into account that here time will be in integer seconds. And do not forget that the sample size is different, it is in BlockSize . - Uladzimir Palekh