I record some sound with NAudio to float array and after getting the recorded audio I want to convert it back from float array to wav to check it.
I wrote a method but I'm not sure about it because: I try to record a 15Khz tone of 5 seconds and pass it through fft to check for the spectrum to ensure the 15k was catched. These are my steps:
I generate this tone with matlab firstly and record with Audacity that it is the right tone. Audacity shows a nice spectrum with a peak in 15k with -30db.
I try to record with Naudio the tone and then converting it back from the recorded float array to wav to check it in Audacity. unfortunately, it show nothing but some noise up to 12k and no more(-80db at 12k).
Here is the regular recording convertion when data is available:
void myWaveIn_DataAvailable(object sender, WaveInEventArgs e)
{
myMemoryStream.Write(e.Buffer, 0, e.BytesRecorded);//this is for playing the myMemoryStream
for (int index = 0; index < e.BytesRecorded; index += 2)//Here I convert in a loop the stream into floating number samples
{
short sample = (short)((e.Buffer[index + 1] << 8) |
e.Buffer[index + 0]);
samples32.Add(sample / 32768f);//IEEE 32 floating number
}
}
This is my code for converting back:
void floatBackToStream(float[] myfloatArray)
{
short[] myShort = new short [myfloatArray.Length];
myMemoryStream2 = new MemoryStream();
for (int i = 0; i < myfloatArray.Length; i++)
{
myShort[i] = (short)(myfloatArray[i] * 32768f);
myMemoryStream2.WriteByte((byte)(myShort[i] & 255));
myMemoryStream2.WriteByte((byte)(myShort[i] >> 8));
}
myMemoryStream2.Position = 0;
myRaw2 = new RawSourceWaveStream(myMemoryStream2, new WaveFormat(44100,1));
WaveFileWriter.CreateWaveFile("C:/Users/alon/Desktop/myRecordings/myCalibration.wav", myRaw2);
}
Ok, so: I created a band-pass filter, with peaks at 15 kHz. Peak value is +80dB since you said that you have -80dB at 12 kHz. If we want filter to work well, it needs to be a high order transfer function. So the equations could get quite long, but it is just multiplication and addition, therefore it won't take a long time for the CPU to perform. I assumed your sampling rate to be 44,1 kHz. The equation is as follows:
y[k]=a7*u[k-1]+a6*u[k-2]+a5*u[k-3]+a4*u[k-4]+a3*u[k-5]+a2*u[k-6]+a1*u[k-7]+a0*u[k-8] -b7*y[k-1]-b6*y[k-2]-b5*y[k-3]-b4*y[k-4]-b3*y[k-5]-b2*y[k-6]-b1*y[k-7]-b0*y[k-8];
Where u
is the measured signal and y
is a new vector that will contain the 15 kHz frequency. Now there are 2 approaches to estimate coefficients:
This is the proper physical equation. Coeffecients:
a7=207.5;
a6=-578.1;
a5=546.6;
a4=-149.8;
a3=-49.39;
a2=22.21;
a1=1.349;
a0=0.006915;
b7=-0.945;
b6=0.3907;
b5=-0.09229;
b4=0.01363;
b3=-0.001288;
b2=0.00007605;
b1=-2.567*10^(-6);
b0=3.79*10^(-8);
For this case we have following coefficients:
a7=3.301;
a6=8.643;
a5=-51.47;
a4=59.35;
a3=-5.498;
a2=-23.56;
a1=8.625;
a0=0.6114;
b7=-5.693;
b6=14.18;
b5=-20.19;
b4=17.96;
b3=-10.22;
b2=3.638;
b1=-0.7397;
b0=0.0658;
Just try both of them and see what you get. Make sure to use a datatype of sufficient precision, to make sure we wont multiply times 0.