This project has moved and is read-only. For the latest updates, please go here.

Generate bandpass filtered white noise

Nov 7, 2012 at 4: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,


Nov 7, 2012 at 4: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.


Nov 7, 2012 at 5:06 PM


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.

Nov 7, 2012 at 5: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 5: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;



		/// <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);

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

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

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

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

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

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

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

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


		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 5:39 PM


I have a PDF that explains a little Biquad Filter

available here


Nov 8, 2012 at 11:04 AM


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,


Nov 8, 2012 at 11:18 AM


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 11:30 AM

Actually, it's quite urgent.

A simple example would be great !!!

Thanks a lot :-)

Nov 8, 2012 at 1: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 ::

Publisher Visual Studio Express 2012

Nov 9, 2012 at 11:46 AM

Thanks ManuN I will check it out ...