Main Archive Specials Wiki | FAQ Links Submit Forum


3 Band Equaliser

References : Posted by Neil C

Notes :
Simple 3 band equaliser with adjustable low and high frequencies ...

Fairly fast algo, good quality output (seems to be accoustically transparent with all gains set to 1.0)

How to use ...

1. First you need to declare a state for your eq

EQSTATE eq;

2. Now initialise the state (we'll assume your output frequency is 48Khz)

set_3band_state(eq,880,5000,480000);

Your EQ bands are now as follows (approximatley!)

low band = 0Hz to 880Hz
mid band = 880Hz to 5000Hz
high band = 5000Hz to 24000Hz

3. Set the gains to some values ...

eq.lg = 1.5; // Boost bass by 50%
eq.mg = 0.75; // Cut mid by 25%
eq.hg = 1.0; // Leave high band alone

4. You can now EQ some samples

out_sample = do_3band(eq,in_sample)


Have fun and mail me if any problems ... etanza at lycos dot co dot uk


Neil C / Etanza Systems, 2006 :)



Code :
First the header file ....
//---------------------------------------------------------------------------
//
// 3 Band EQ :)
//
// EQ.H - Header file for 3 band EQ
//
// (c) Neil C / Etanza Systems / 2K6
//
// Shouts / Loves / Moans = etanza at lycos dot co dot uk
//
// This work is hereby placed in the public domain for all purposes, including
// use in commercial applications.
//
// The author assumes NO RESPONSIBILITY for any problems caused by the use of
// this software.
//
//----------------------------------------------------------------------------

#ifndef __EQ3BAND__
#define __EQ3BAND__


// ------------
//| Structures |
// ------------

typedef struct
{
// Filter #1 (Low band)

double lf; // Frequency
double f1p0; // Poles ...
double f1p1;
double f1p2;
double f1p3;

// Filter #2 (High band)

double hf; // Frequency
double f2p0; // Poles ...
double f2p1;
double f2p2;
double f2p3;

// Sample history buffer

double sdm1; // Sample data minus 1
double sdm2; // 2
double sdm3; // 3

// Gain Controls

double lg; // low gain
double mg; // mid gain
double hg; // high gain

} EQSTATE;


// ---------
//| Exports |
// ---------

extern void init_3band_state(EQSTATE* es, int lowfreq, int highfreq, int mixfreq);
extern double do_3band(EQSTATE* es, double sample);


#endif // #ifndef __EQ3BAND__
//---------------------------------------------------------------------------

Now the source ...
//----------------------------------------------------------------------------
//
// 3 Band EQ :)
//
// EQ.C - Main Source file for 3 band EQ
//
// (c) Neil C / Etanza Systems / 2K6
//
// Shouts / Loves / Moans = etanza at lycos dot co dot uk
//
// This work is hereby placed in the public domain for all purposes, including
// use in commercial applications.
//
// The author assumes NO RESPONSIBILITY for any problems caused by the use of
// this software.
//
//----------------------------------------------------------------------------

// NOTES :
//
// - Original filter code by Paul Kellet (musicdsp.pdf)
//
// - Uses 4 first order filters in series, should give 24dB per octave
//
// - Now with P4 Denormal fix :)


//----------------------------------------------------------------------------

// ----------
//| Includes |
// ----------

#include
#include "eq.h"


// -----------
//| Constants |
// -----------

static double vsa = (1.0 / 4294967295.0); // Very small amount (Denormal Fix)


// ---------------
//| Initialise EQ |
// ---------------

// Recommended frequencies are ...
//
// lowfreq = 880 Hz
// highfreq = 5000 Hz
//
// Set mixfreq to whatever rate your system is using (eg 48Khz)

void init_3band_state(EQSTATE* es, int lowfreq, int highfreq, int mixfreq)
{
// Clear state

memset(es,0,sizeof(EQSTATE));

// Set Low/Mid/High gains to unity

es->lg = 1.0;
es->mg = 1.0;
es->hg = 1.0;

// Calculate filter cutoff frequencies

es->lf = 2 * sin(M_PI * ((double)lowfreq / (double)mixfreq));
es->hf = 2 * sin(M_PI * ((double)highfreq / (double)mixfreq));
}


// ---------------
//| EQ one sample |
// ---------------

// - sample can be any range you like :)
//
// Note that the output will depend on the gain settings for each band
// (especially the bass) so may require clipping before output, but you
// knew that anyway :)

double do_3band(EQSTATE* es, double sample)
{
// Locals

double l,m,h; // Low / Mid / High - Sample Values

// Filter #1 (lowpass)

es->f1p0 += (es->lf * (sample - es->f1p0)) + vsa;
es->f1p1 += (es->lf * (es->f1p0 - es->f1p1));
es->f1p2 += (es->lf * (es->f1p1 - es->f1p2));
es->f1p3 += (es->lf * (es->f1p2 - es->f1p3));

l = es->f1p3;

// Filter #2 (highpass)

es->f2p0 += (es->hf * (sample - es->f2p0)) + vsa;
es->f2p1 += (es->hf * (es->f2p0 - es->f2p1));
es->f2p2 += (es->hf * (es->f2p1 - es->f2p2));
es->f2p3 += (es->hf * (es->f2p2 - es->f2p3));

h = es->sdm3 - es->f2p3;

// Calculate midrange (signal - (low + high))

m = es->sdm3 - (h + l);

// Scale, Combine and store

l *= es->lg;
m *= es->mg;
h *= es->hg;

// Shuffle history buffer

es->sdm3 = es->sdm2;
es->sdm2 = es->sdm1;
es->sdm1 = sample;

// Return result

return(l + m + h);
}


//----------------------------------------------------------------------------



Comments


Added on : 27/03/07 by yuri_xl[ AT ]tom[ DOT ]com
Comment :
Great Thanks!
I have one problem the below:
  double  f2p0;     // Poles ...
  double  f2p1;
  double  f2p2;
  double  f2p3;
that I want to know the starting value
about f2p0,f2p1,...!
  




Added on : 14/04/07 by james_braun_gottvater_der_funk[ AT ]yahoo[ DOT ]com
Comment :
yuri:

The invocation of memset() during the initialization method sets all the the members of the struct to zero.  




Added on : 22/05/07 by hellmanc[ AT ]hotmail[ DOT ]com
Comment :
This is great -- I want to develop a compressor/limiter/expander and have been looking long and hard for bandpass / eq filtering code.  Here it is!

I am sure we could easily expand this into an x band eq.

Thanks!




Added on : 05/07/07 by tom tom
Comment :
Hi !

I've just transposed your code under Delphi.

It works well if the gain is under 1, but if i put gain > 1 i get clipping (annoying sound clips), even at 1.1;

Is it normal ?

I convert my smallint (44100 16 bits) to double before process, and convert the obtained value back to smallint with clipping (if < -32768 i set it to -32768, and if > 32768 i set it to 32768).

What did i do wrong ?

Regards

Tom




Added on : 21/07/07 by herbert7[ AT ]gmx[ DOT ]de
Comment :
Hi.

Maybe the answer is quite easy. The upper limit is 32767 not 32768.

Regards

Herbert




Added on : 22/08/07 by angga0017163[ AT ]yahoo[ DOT ]com
Comment :
Hi, Can U send me a full source code for this 3 band state eq from start to end ??
Please !!!!
I really need it for my study in school.
I hope you can send me, to my email.

thanks you.
regard


angga




Added on : 05/05/09 by vhain6512[ AT ]gmail[ DOT ]com
Comment :
How can I expand this 3 Band EQ into X Band EQ..?!

Anybody answer me, or email me..




Added on : 22/05/09 by bob[ AT ]yahoob[ DOT ]com
Comment :
For more bands, you could take the low-pass and repeat the process on that.



Added on : 23/05/09 by philip[ AT ]blastbay[ DOT ]com
Comment :
This is a great little filter, I am using it in an application but when I first started playing with it I noticed some problems. The mid range didn't seem to be calculated properly, a friend of mine who knows more about dsp than I do took a quick look at it and suggested the following change:

  m          = es->sdm3 - (h + l);

Should be:

  m          = sample - (h + l);

I've tested it with this small fix and everything works perfectly now. Just thought I'd bring this to your attention... Thanks for a great code snippet!              




Added on : 24/05/09 by bob[ AT ]yahoob[ DOT ]com
Comment :
What problems were you getting? Doesn't removing the delay cause phase problems?




Added on : 25/06/09 by bechar[ DOT ]ce[ AT ]ygamil[ DOT ]com
Comment :
Hi Great Stuff,

how to create 6 band equalizer, is any algorithm for 6 band same like 3 band, please help me if any one

thanks in advance




Added on : 06/05/10 by wijesena[ AT ]gmail[ DOT ]com
Comment :
How to extend this to 6 band equalizer?              



Added on : 27/05/10 by marian_sabianaa[ AT ]hotmail[ DOT ]com
Comment :
hello! thanks for your code!!
i tried to use the code in my project of guitar distortions in real time (in C) and i could'nt, i'm in linux using jack audio server, and it starts to have x-runs everytime i turn on the equalizer. do you any idea of how solving this? (from 5 to 50 milliseconds o x-runs)
i was thinking of coding it in assembler but i don't know if that would be the solution.
excuse me for my english, i'm from argentina and it's been a while since i last wrote in this language!
thanks in advance, hoping to see any answer!
mariano




Added on : 03/11/10 by word_65[ AT ]yahoo[ DOT ]com
Comment :
Dev c++ can not run it? any suggesstions, how to run it?



Added on : 23/02/11 by vincent[ DOT ]bruinink at gmail dot com
Comment :
This example is exactly what i've been looking for. This little piece of code executes faster then the one I have been using before.

FYI,

I will use the code in a 3 band compressor / limiter / clipper for FM broadcasting.

Thanks for sharing.

Best regards,

Vincent Bruinink.      




Added on : 21/03/11 by ptm[ DOT ]listmail[ AT ]gmail[ DOT ]com
Comment :
I've got this filtering audio on iOS by running my sample through do_3band in the render callback. However, I'm getting a fair amount of distortion. Here's an example with my EQ3Band gains all set to 1.0:

http://www.youtube.com/watch?v=W_6JaNUvUjA

Here's the code for my implementation:

https://github.com/tassock/mixerhost/commit/4b8b87028bfffe352ed67609f747858059a3e89b

I assume others using this aren't having this same distortion issue? If so, what sort of audio sample formats are you using (big/little endian, float/integer samples, etc). Thanks!




Added on : 30/04/11 by baranes[ DOT ]iphone[ AT ]gmail[ DOT ]com
Comment :
hi,
I got also distorsion even though my all my gains are set to 1. Please help

Lucie




Added on : 05/06/12 by docdiablos[ AT ]gmail[ DOT ]com
Comment :
      I've made an implementation of 3 band Equalizer to read a wave file,apply filtering and then save wave  outputfile.
With your code I have a lot of distortion,I think the problem maybe coefficient calculation:

es->f1p0  += (es->lf * (sample   - es->f1p0))+vsa ;
  ...



Can anyone resolve distortion?
        




Added on : 11/01/13 by johan[ AT ]klevgrand[ DOT ]se
Comment :
              Works fine for me on iOS. Maybe you feed a interleaved stereo signal with the same EQSTATE instance (you'll need one EQSTATE for each channel)?



Added on : 07/04/13 by noemail[ AT ]righthere[ DOT ]at
Comment :
It's all about WHERE you init you EQ.  Try a little :) If you don't find out yourself, I'll help you.



Add your own comment
Comments are displayed in fixed width, no HTML code allowed!
Email:

Comment:

Are you human?



Site created and maintained by Bram
Graphic design by line.out | Server sponsered by fxpansion