This project has moved. For the latest updates, please go here.

Generate bandpass filtered white noise

Nov 7, 2012 at 3:44 PM

Hi Mark,

I'm using your NAudio library to generate acoustical stimulations in a psychophysic experiment.

By the way, Great work !!!

I need to generate a white noise filtered between 800Hz-1200Hz.

Generating a white noise is quite simple.

But how can I filter it to the requested frequency band?

 

Thanks in advance,

Ron

Coordinator
Nov 7, 2012 at 3:47 PM

I'm afraid NAudio doesn't come with a bandpass filter (I'd like to include some filters in the future). What I normally do in cases like this is find some C code that does the filtering (there are good sites out there that will work out filter coefficients and even generate the code), and then port it to C#. If you are using the ISampleProvider interface, this should be quite straightforward, as you'll just be processing samples as 32 bit IEEE floats as they come through your Read method.

Mark

Nov 7, 2012 at 4:06 PM

Hello,

If I can afford , 

you can use the example in BiQuadFilter.

It just takes a little change for use with iSample.

I use it for multiple parametric Equalisers.

I tested LowPass and HiPass and they functioned properly.

Coordinator
Nov 7, 2012 at 4:08 PM

ah yes, I forgot that was still in there :) I want to get this converted to ISampleProvider for the next NAudio

Nov 7, 2012 at 4:29 PM

 

This is an example of using the class Biquad Filter

I do not use the primary function "Transform" the class BiQuadFilter

using NAudio.Dsp;
using System;


namespace NAudio.Effects
{

	public class EqFilterX
	{


		BiQuadFilter2 bqf;

		float[] sIn = new float[0];    // Sample In [n]
		float[] sIn1 = new float[0];   // Sample In [n-1]
		float[] sIn2 = new float[0];   // Sample In [n-2]
		float[] sOut = new float[0];   // Sample Out [n] 
		float[] sOut1 = new float[0];  // Sample Out [n-1]
		float[] sOut2 = new float[0];  // Sample Out [n-2]

		public EqFilterX()
			: this(BiQuadFilterType.LowPassFilter, 100.0f, 1.0f, 1.0f, false)
		{
		}

		public EqFilterX(BiQuadFilterType filtertype, float frequency, float q, float gain, bool bypass)
		{
			Bypass = bypass;
			FilterType = filtertype;
			Frequency = frequency;
			Q = q;
			dBGain = gain;

			OnParamChanges();


		}

		/// <summary>
		/// Type de Filter
		/// </summary>
		public BiQuadFilterType FilterType { get; set; }

		/// <summary>
		/// CenterFrequence or Frequence 
		/// </summary>
		public float Frequency { get; set; }

		/// <summary>
		/// Q or Shelf Slope or BandWidth
		/// </summary>
		public float Q { get; set; }

		/// <summary>
		/// Gain
		/// </summary>
		public float dBGain { get; set; }

		///// <summary>
		///// Numbre of channel
		///// </summary>
		//public int ChannelMax { get; set; }


		public override void OnParamChanges()
		{
			sIn = new float[ChannelMax];    // Sample In [n]
			sIn1 = new float[ChannelMax];   // Sample In [n-1]
			sIn2 = new float[ChannelMax];   // Sample In [n-2]
			sOut = new float[ChannelMax];   // Sample Out [n] 
			sOut1 = new float[ChannelMax];  // Sample Out [n-1]
			sOut2 = new float[ChannelMax];

			switch (FilterType)
			{
				case BiQuadFilterType.AllPassFilter:
					bqf = BiQuadFilter2.AllPassFilter(SampleRate, Frequency, Q);
					break;

				case BiQuadFilterType.BandPassFilterConstantPeakGain:
					bqf = BiQuadFilter2.BandPassFilterConstantPeakGain(SampleRate, Frequency, Q);
					break;

				case BiQuadFilterType.BandPassFilterConstantSkirtGain:
					bqf = BiQuadFilter2.BandPassFilterConstantSkirtGain(SampleRate, Frequency, Q);
					break;

				case BiQuadFilterType.HighPassFilter:
					bqf = BiQuadFilter2.HighPassFilter(SampleRate, Frequency, Q);
					break;

				case BiQuadFilterType.HighShelf:
					// Q = Shelf slope
					bqf = BiQuadFilter2.HighShelf(SampleRate, Frequency, Q, dBGain);
					break;

				case BiQuadFilterType.LowPassFilter:
					bqf = BiQuadFilter2.LowPassFilter(SampleRate, Frequency, Q);
					break;

				case BiQuadFilterType.LowShelf:
					// Q = Shelf slope
					bqf = BiQuadFilter2.LowShelf(SampleRate, Frequency, Q, dBGain);
					break;

				case BiQuadFilterType.NotchFilter:
					bqf = BiQuadFilter2.NotchFilter(SampleRate, Frequency, Q);
					break;

				case BiQuadFilterType.PeakingEQ:
					bqf = BiQuadFilter2.PeakingEQ(SampleRate, Frequency, Q, dBGain);
					break;

				default:
					break;
			}
		}


		public override void Sample(ref float sample, ref int channel)
		{

			

			sIn[channel] = sample;

			sOut[channel] = (float)((bqf.b0 / bqf.a0) * sIn[channel] + (bqf.b1 / bqf.a0) * sIn1[channel] + (bqf.b2 / bqf.a0) * sIn2[channel]
							- (bqf.a1 / bqf.a0) * sOut1[channel] - (bqf.a2 / bqf.a0) * sOut2[channel]);
			sIn2[channel] = sIn1[channel];
			sIn1[channel] = sIn[channel];

			sOut2[channel] = sOut1[channel];
			sOut1[channel] = sOut[channel];

			if (Bypass == false)
				sample = sOut[channel];



		}


simply declare the class.

Initialize filters (Frequency, SampleRate, Gain, Q, Type, ...) and call the function "OnParamChanges"

Then in a loop passes each sample in the function "Sample"

This example is an excerpt from one of my class "Effects".

it is possible to use Eq's different for channels.

Of course you need to adapt this example.

Nov 7, 2012 at 4:39 PM

 

I have a PDF that explains a little Biquad Filter

available here

WeTranfert.

Nov 8, 2012 at 10:04 AM

Hi,

Thanks for the quick response.

Unfortunately, I don't have much knowledge in sound programming, filters and all the fourier stuff (pardon).

I need to generate a white noise filtered between 800-1200Hz for a 1 sec period.

What would be the easiest way to do it?

How can I use the EqFilterX class? How do I need to set the parameters? Can you attach a sample code?

By the way, where can I find the BiQuadFilter2 class? and what is the difference from the NAudio BiQuadFilter class?

Thanks again,

Ron

Nov 8, 2012 at 10:18 AM

Hello,

gloupsss BiQuadFilter2 is exactly the same as BiQuadFilter


  BiQuadFilter is located in the DSP library NAudio


If it's urgent I can make an example of you (depending on my available time), or you can just wait to be the complement of Mark.

Nov 8, 2012 at 10:30 AM

Actually, it's quite urgent.

A simple example would be great !!!

Thanks a lot :-)

Nov 8, 2012 at 12:25 PM

Here I have an example.

Well I took an existing generator and add LowPassFilter & HighPassFilter for Pink Noise & WhiteNoise and duration

I did some test with an analyzer (SmaartLive). this example seems correct

I use as a reference the dll NAudio 1.6.


If you want a more powerful filter, you will have to use FFT.

And not enough time to make an example.


example is available in WeTranfert here ::

https://www.wetransfer.com/dl/N7a9W05a/dede49dd367271447614f812590cb0b1eb5b5db0d9fb2020662074973bd5768975ff26506369f74


Publisher Visual Studio Express 2012

Nov 9, 2012 at 10:46 AM

Thanks ManuN I will check it out ...