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.



Added on : 18/06/13 by umair_maani[ AT ]hotmial[ DOT ]com
Comment :
I added this code to my xcode project. But where i can pass the values and method. May be its funny question for you guyz but please help me. Its loking good to me but in xcode , i am using avaudio player for play sound and making sound app.
Thanks            




Added on : 01/07/13 by ibanzai[ AT ]gmail[ DOT ]com
Comment :
The distortion will occur of you are trying to adapt this to stereo and do so by simply adding an outer loop per channel. Doing so will cause the filter values to compound and cause distortion. Instead, you will need to duplicate all the filter values and keep them separate from the other channel.



Added on : 21/08/13 by paul[ DOT ]brun[ AT ]gmail[ DOT ]com
Comment :
Any good x-band equalizer equivalents for C# that I can use?              



Added on : 09/07/14 by caosansansad[ AT ]yahoo[ DOT ]com
Comment :
I found a lot of information here to make this actually best for all newbie here. Thank you for this information. Really useful! http://www.friv2hi.com



Added on : 05/08/14 by kalle[ DOT ]muenster[ AT ]gmx[ DOT ]de
Comment :
s'cool thanks!
have portet to c#
works fine!





Added on : 19/08/14 by pdp1987[ AT ]rediffmail[ DOT ]com
Comment :
              thanks



Added on : 30/09/14 by manhpr1[ AT ]gmail[ DOT ]com
Comment :
              I appreciate the information you share it really gives me great many advantages in the job. Thanks for a great article.<a href="http://www.y8y8y8y8.net/">Y8Y8</a>



Added on : 30/09/14 by manhpr1[ AT ]gmail[ DOT ]com
Comment :
              I appreciate the information you share it really gives me great many advantages in the job. Thanks for a great article.<a href="http://www.y8y8y8y8.net/">Y8Y8</a>



Added on : 25/05/15 by kbugeja14[ AT ]gmail[ DOT ]com
Comment :
I really need help. I am write an equalizer in c. Is this good for stereo? I need to know how to store a wav file in an array in c. any help? Thanks!



Added on : 28/09/15 by koos[ AT ]rapidaccess[ DOT ]co[ DOT ]za
Comment :
              
Converted Neil's C Code 3 band equalizer to Delphi class, for those who are interested.


1. Create instance of class
   Public
     eq:TEQ;

2. On form create

    eq := TEq.Create;

    //Initialize
    init_3band_state(880,5000,44100,50,-25,0);

    //init_3band_state(lowfreq,highfreq,mixfreq:integer;BassGain,MidGain,HighGain:Double);


3. process: pass Raw 16Bit PCM to eq

eq.Equalize(const Data: Pointer; DataSize: DWORD);

Works like a charm form me

***********************************************
  TEq=Class
  private
    lf,f1p0,f1p1,f1p2,f1p3:double;
    hf,f2p0,f2p1,f2p2,f2p3:double;
    sdm1,sdm2,sdm3:double;
    vsa:double;
    lg,mg,hg:double;
    Function do_3band(sample:Smallint):Smallint;
  public
    constructor create;
    destructor destroy;override;
    procedure init_3band_state(lowfreq,highfreq,mixfreq:integer;BassGain,MidGain,HighGain:Double);
    procedure Equalize(const Data: Pointer; DataSize: DWORD);
  end;


constructor TEQ.create;
begin
  inherited create;
  vsa := (1.0 / 4294967295.0);
  lg := 1.0;
  mg := 1.0;
  hg := 1.0;

end;


destructor TEQ.destroy;
begin
  inherited destroy;
end;

procedure TEQ.init_3band_state(lowfreq,highfreq,mixfreq:integer;BassGain,MidGain,HighGain:Double);
begin

{(880,5000,44100,1.5,0.75,1.0)
eq.lg = 1.5; // Boost bass by 50%
eq.mg = 0.75; // Cut mid by 25%
eq.hg = 1.0; // Leave high band alone }

lg := 1+(BassGain/100);
mg := 1+(MidGain/100);
hg := 1+(HighGain/100);

// Calculate filter cutoff frequencies

lf := 2 * sin(PI * (lowfreq / mixfreq));
hf := 2 * sin(PI * (highfreq / mixfreq));
end;

Function TEQ.do_3band(sample:Smallint):Smallint;
var l,m,h:double;
    res:integer;
begin

// Filter #1 (lowpass)

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

  l := f1p3;

// Filter #2 (highpass)

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

  h := sdm3 - f2p3;

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

  m := sdm3 - (h + l);

// Scale, Combine and store

  l := l * lg;
  m := m * mg;
  h := h * hg;

// Shuffle history buffer

  sdm3 := sdm2;
  sdm2 := sdm1;
  sdm1 := sample;

// Return result
  res := trunc(l+m+h);
  if res > 32767 then res := 32767 else if res < -32768 then res := -32768;

  result := res;
end;

procedure TEQ.Equalize(const Data: Pointer; DataSize: DWORD);
var pSample: PSmallInt;
begin
  pSample := Data;
  while DataSize > 0 do
  begin
    pSample^ := do_3band(pSample^);
    Inc(pSample);
    Dec(DataSize, 2);
  end;
end;




Added on : 15/01/17 by ihiduurol[ AT ]mailer[ DOT ]printemailtext[ DOT ]com
Comment :
[url=http://dutasterideavodartonline.org/]dutasterideavodartonline.org.ankor[/url] <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/



Added on : 16/01/17 by eezunawu[ AT ]email[ DOT ]eamale[ DOT ]com
Comment :
[url=http://dapoxetine-onlinepriligy.net/]dapoxetine-onlinepriligy.net.ankor[/url] <a href="http://ventolinsalbutamol-buy.org/">ventolinsalbutamol-buy.org.ankor</a> http://ventolinsalbutamolbuy.org/



Added on : 16/01/17 by ecucad[ AT ]email[ DOT ]eamale[ DOT ]com
Comment :
[url=http://dapoxetine-onlinepriligy.net/]dapoxetine-onlinepriligy.net.ankor[/url] <a href="http://ventolinsalbutamol-buy.org/">ventolinsalbutamol-buy.org.ankor</a> http://ventolinsalbutamolbuy.org/



Added on : 27/02/17 by pavlik[ DOT ]login[ DOT ]72[ AT ]mail[ DOT ]ru
Comment :
çàêàçàòü ïðîäâèæåíèå èíòåðíåò ðåñóðñà ëîãèí ñêàéïà pokras7777



Added on : 21/04/17 by haroldoccuh[ AT ]mail[ DOT ]ru
Comment :
[url=http://sildenafil100mgrezeptfreikaufen.com/]sildenafil hennig 100mg 24 stück [/url]
<a href=" http://sildenafil100mgrezeptfreikaufen.com/ ">sildenafil 100mg 24 stück ratiopharm </a>




Added on : 21/04/17 by donaldemoff[ AT ]mail[ DOT ]ru
Comment :
[url=http://cialisbilligkaufenohnerezept.com/]cialis günstig kaufen forum [/url]
<a href=" http://cialisbilligkaufenohnerezept.com/ ">cialis generika günstig kaufen </a>




Added on : 22/04/17 by antoineber[ AT ]mail[ DOT ]ru
Comment :
[url=http://acheteramoxicillinepascherenfrance.com/]acheter amoxicilline pas cher en france [/url]
<a href=" http://acheteramoxicillinepascherenfrance.com/ ">acheter amoxicilline acide clavulanique </a>




Added on : 22/04/17 by darrellstire[ AT ]mail[ DOT ]ru
Comment :
[url=http://acheterclomidenlignefrance.com/]achat clomid 100mg [/url]
<a href=" http://acheterclomidenlignefrance.com/ ">acheter clomid sans ordonnance en france </a>




Added on : 22/04/17 by stanleybrown[ AT ]mail[ DOT ]ru
Comment :
[url=http://achatclomidsurinternet.com/]achat clomid livraison rapide [/url]
<a href=" http://achatclomidsurinternet.com/ ">acheter clomid sans ordonnance en france </a>




Added on : 26/04/17 by yxipxketcoweqoj[ AT ]mail[ DOT ]ru
Comment :
[url=http://lasixohnerezeptkaufen.com/]lasix ohne [/url]
<a href=" http://lasixohnerezeptkaufen.com/ ">lasix ohne rezept kaufen </a>




Added on : 26/04/17 by wgidekyvoruqiwe[ AT ]mail[ DOT ]ru
Comment :
[url=http://furosemidkaufenohnerezept.com/]furosemid billig kaufen [/url]
<a href=" http://furosemidkaufenohnerezept.com/ ">furosemid 40 kaufen </a>




Added on : 26/04/17 by edwardpooda[ AT ]mail[ DOT ]ru
Comment :
[url=http://kamagraoraljellybilligurisikoloskaufen.com/]kamagra oral jelly 100 mg wie einnehmen [/url]
<a href=" http://kamagraoraljellybilligurisikoloskaufen.com/ ">kamagra oral jelly 100mg 1 week pack </a>




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