views:

227

answers:

1

I'm trying to implement a DFT-based 8-band equalizer for the sole purpose of learning. To prove that my DFT implementation works I fed an audio signal, analyzed it and then resynthesized it again with no modifications made to the frequency spectrum. So far so good.

I'm using the so-called 'standard way of calculating the DFT' which is by correlation. This method calculates the real and imaginary parts both N/2 + 1 samples in length. To attenuate a frequency I'm just doing:

float atnFactor = 0.6;
Re[k] *= atnFactor;
Im[k] *= atnFactor;

where 'k' is an index in the range 0 to N/2, but what I get after resynthesis is a slighty distorted signal, especially at low frequencies.

The input signal sample rate is 44.1 khz and since I just want a 8-band equalizer I'm feeding the DFT 16 samples at a time so I have 8 frequency bins to play with.

Can someone show me what I'm doing wrong? I tried to find info on this subject on the internet but couldn't find any.

Thanks in advance.

+3  A: 

DFT and FFT are essentially the same for the purposes of this question.

To attenuate a frequency bin (or "band") in an FFT-transformed array, you need to multiply both the real and imaginary components by the same factor, and also multiply the real and imaginary components of the corresponding negative frequency bin. FFT produces a transformed pair of arrays where the first half of the values represent positive frequency components and the second half represents negative frequency components.

Here is a simplified code sample for a low-pass filter that explains what I mean:

// fftsize = size of fft window
int halfFFTsize = fftsize / 2;
float lowpassFreq1 = 1000.0;
float lowpassFreq2 = 2000.0;
for (int i = 0; i < halfFFTsize; i++)
{
    int ineg = fftsize - 1 - i; // index of neg. freq.
    float freq = (float)i * (44100.0F / (float)halfFFTsize);
    if (freq >= lowpassFreq2)
    {
        real[i] = 0;
        imag[i] = 0;
        real[ineg] = 0;
        imag[ineg] = 0;
    }
    else if (freq >= lowpassFreq1)
    {
        float mult = 1.0 - ((freq - lowpassFreq1) / 
            (lowpassFreq2 - lowpassFreq1));
        real[i] *= mult;
        imag[i] *= mult;
        real[ineg] *= mult;
        imag[ineg] *= mult;
    }

}

Update: after reading your edit, I'd have to say your code is working as expected. I assumed you were getting a massively distorted re-synthesized signal, not a "slighty distorted signal, especially at low frequencies".

I think the distortion you're seeing is the result of the very small window size you're using - this would especially be the case if you're not using a Hanning window approach to reconstruct the original signal.

Try running your code with a more typical window size (like 1024). An 8-band equalizer doesn't usually use an 8-bin FFT window. Typically, the settings of 8 sliders would be used to calculate a curvy function connecting the 8 points in the frequency domain, and this function would then be used to set the bin amplitudes for a much larger, more finely-grained set of frequencies.

One more point, too: the frequency bins divide up the available range evenly, so no matter how big your window size is, more than half of the bins cover frequencies that aren't audible to the human ear. This is why the bands covered by an equalizer are typically scaled logarithmically (e.g. 100Hz, 1Khz and 10Khz for a typical 3-band equalizer) and thus do not apply to equal numbers of frequency bins.

In the case of an evenly-spaced 8 bin window, attenuuation of 5 of the 8 is certain to have no audible effect other than distortion of the audible frequencies.

MusiGenesis
+1. Your last sentence is particularly important. Filtering by setting values in the frequency domain to zero is in effect applying a square filter, which will cause substantial ringing (Gibbs phenomenon) in the frequency response. Keep in mind that setting a value in the discrete frequency domain to zero only affects the response at that exact frequency. The frequency response between frequency samples will oscillate wildly with a brick wall filter. I suggest you look into tutorials on band-pass FIR design.
Jason
@Jason: do you have links to any good filter design tutorials? I took a brief plunge into coding filters like my above sample, but I didn't get much further than this.
MusiGenesis
Oh I guess you removed that sentence, but you also changed your code to show a more gradual change in the passband.
Jason
@Jason: I'm sure my rampdown version here still has some problems with it, and I'd be very grateful for any info you have on this sort of thing. I was using filters like these in a software synthesizer, but not on the basis of any real knowledge of what I was doing.
MusiGenesis
https://ccrma.stanford.edu/~jos/sasp/FIR_Digital_Filter_Design.html looks pretty good, but I have not personally used many online tutorials since I have a lot of textbooks. Probably the simplest method is the window design method, but anymore I just use optimal methods of filter design like Parks-McClellan to create filters. This algorithm is pretty complex, but it is widely available. It will output the FIR filter coefficients based on a number of input parameters that you provide.
Jason
https://ccrma.stanford.edu/~jos/sasp/Generalized_Window_Method.html gives a step-by-step instruction for the window design method.
Jason
If you have access to MATLAB, look into the remez function (or firpm in newer versions). If you don't have MATLAB, I think octave has the same capabilities. A lot of time, I just use MATLAB or Octave to create the filter coefficients since they have the nice optimal filter design functions, and then use the filter in a C or C++ program.
Jason
If I understood you correctly, I wouldn't need to modify any negative frequency bins since I calculate the DFT using correlation and I get two arrays Re[] and Im[] that are both N/2 samples in length. Am I correct?
Trap
If you are only getting N/2 complex samples out, you are not performing the complete DFT which is defined to return the same number of same number of samples as input. Sometimes only half of the samples are calculated since for real input signals, the negative half is a mirror image of the positive half. However, if you want to filter and resynthesize, you need to use all of the frequency samples.
Jason