Hamming window generates a line

1.4k views Asked by At

I have an audio file and read all data from the sound card buffer. Then, I convert byte[] to float[] to use them for hamming window. The waveform of the audio is:

https://i.stack.imgur.com/2NhTB.png

after using hamming window:

https://i.stack.imgur.com/N87qE.png

is the waveform of audio with hamming window right? Where is my mistake?

by the way i use naudio library to process audio:

WaveChannel32 wave = new WaveChannel32(new WaveFileReader("sesDosyası.wav"));
byte []buffer = new byte[wave.length];
float []data = new float[wave.length / 4];
int read = wave.Read(buffer, 0, wave.length);
for (int i = 0; i < read / 4; i++)
{   
    data[i] = BitConverter.ToSingle(buffer, i * 4); //converting byte to float
    chart1.Series["wave"].Points.Add(data[i]); //first waveform
}

for (int j = 0; j < read/4; j++)
{
   data[j] = (float)(0.54 - 0.46 * Math.Cos((2 * Math.PI * data[j]) / (read / 4 - 1)));//hamming
   chart2.Series["wave"].Points.Add(data[j]); //second waveform
}
2

There are 2 answers

8
AShelly On BEST ANSWER

It appears you are applying the window to the whole wave, so read is going to be huge, so the term inside the cos is always going to be very near 0 for a data between [-1,1].

So you are always getting .54 - .46*cos(0) = .54 - .46*1.0 = .08

From wikipedia, only j should be inside the cosine - that gives the window, which you then multiply by data[j]:

window =  0.54 - 0.46 * Math.Cos( (2*Math.PI * j)/(total - 1) ).
hammed_signal = data[j]*window;

Why are you trying to apply a hamming window to the whole wave?

0
Pondidum On

I think your hamming line is wrong:

data[j] = (float)(0.54 - 0.46 * Math.Cos((2 * Math.PI * data[j]) / (read - 1)));

As your loop goes from 0 to read/4, and you are dividing by read, so if you have read 16 samples, your for loop looks only at the first 4, but divides by 15, not 3.

var total = read / 4;

for (int j = 0; j < total; j++)
{
   data[j] = (float)(0.54 - 0.46 * Math.Cos((2 * Math.PI * data[j]) / (total- 1)));//hamming
   chart2.Series["wave"].Points.Add(data[j]); //second waveform
}