1
votes

I perform FFT-IFFT in order to take out 50 Hz and its harmonics from my signal, using Matlab. For this purpose I break down my signal into windows of 1024 samples, and perform FFT on it. I do overlapping of 50% as well. After FFT is done, I take out those harmonics, and do IFFT in order to get filtered data. My question is: How to sum up all those windows with overlaps to get a signal?

my code is below. As you can see, I perform FFT-IFFT on each window and do not know how to get all windows back together.

[y, Fs, nbits] = wavread([fileName]);                    %read the data
[noSamples, noChannels] = size(y);
N = 1024;                                                 %window length 2^10
winLength=N; 
Fres = Fs/N;                                              % resolution frequency
nofWins = floor(noSamples/winLength);                     % No of full windows
noWins = round((100/50)*nofWins - 1);                     % rounded no of windows
yPaddedLength = floor(noWins*0.5*winLength + winLength);  % padding wth 0
yZeroPadded =[y zeros(1, (yPaddedLength - noSamples))];   % padded signal y
nofWinsPadded = round(yPaddedLength/winLength);
noWinsPadded = round((100/50)*nofWinsPadded - 1);         % no of padded windows

odd = true;
for k = 1:(noWinsPadded-1)
    j = floor(0.5*k);
    at = j*winLength + 1;
    overlapWinLength=floor(0.5*winLength);
    range = at:(at + winLength - 1);

if odd
    data = yZeroPadded(range, 1);
    data_sum=sum(data);                                    % from now on - to perform 
                                                           % DC removal
    data_average=data_sum/N;
    data=data-data_average;

else
   data = yZeroPadded(range+overlapWinLength, 1);
    data_sum=sum(data);
    data_average=data_sum/N;
    data=data-data_average;
end;

odd=~odd;
    spectrum = fft(data);
    F=length(spectrum);
    F=spectrum;
    F(10:11)=zeros;                         % FFT No equals to zero removes harmonics
    F(17:18)=zeros                          % and so on
filtered_signal=IFFT(F);

Thanking you in anticipation, Elen Che

2

2 Answers

2
votes

Filtering in the frequency domain is tricky at best, and hazardous at worst. A better method is to use a time-domain filter. I have roughly outlined the reasons here.

If you are trying to eliminate 50Hz line-power hum from audio, a better approach is to use a notch filter. Try a second order Chebyshev band-stop filter, which I believe can be easily designed in MATLAB. You could also try a 3rd or 4th order Butterworth band-stop filter. (those orders and types are just off the top of my head based on some experience). You will use one band for each harmonic and you can use Matlab functions that apply the filter non-causally, so it won't effect the phase of your data.

1
votes

If all you're doing is filtering, then you shouldn't be creating overlapping input windows.

Once you've fixed that, then a common method to reconstruct is overlap-and-add.