I am processing the signal from MPU6050. Applying FIR filter in the frequency domain and then taking IFFT to get filtered signal but getting some spikes in the output signal. Searched a lot about it and found here exit spectral leakage. Found some solutions like zero paddings and windowing but nothing is working. Can some one take a look and let me know what I am doing wrong here.

Here is what I am getting Filtering result after IFFT

Some zoom in Zoom in graph

More zoom in

Zoom in graph

Compete MATLAB Code

close all; clear all ;clc; delete(instrfindall); 
arduino=serial('COM6','BAUD', 115200); 
java.lang.Thread.sleep(0.01);  % in mysec!

samples = pow2(nextpow2(5000))
sig = []; out_sig =[0];  tic;    G=0;    trying=0;  pingpong = 0;
while 1
    idn = fscanf(arduino);
    xx = str2double(idn);
    if isnan(xx)
        java.lang.Thread.sleep(1);  % in mysec!
        java.lang.Thread.sleep(1);  % in mysec!
        trying  = trying + 1
        sig = [sig  xx];
    if length(sig)>samples - 1
plot(sig,'g');      %---------------------------->Orginal signal <---------------------------
hold on 
PG = plot(out_sig,'k');  %---------------------------->Output signal <---------------------------
PG.YDataSource = 'out_sig';

SegmentLength = pow2(nextpow2(1000))  % Transform length to next pow of 2
Fs = SegmentLength; 

wiin = hann(64,'symmetric');
filtcoeff  = fir1(63, 20/Fs, 'low', wiin, 'scale');
firfilterimpresp = impz(filtcoeff);
filterffte = fft(firfilterimpresp,SegmentLength); % FFT of impulse response of filter/system

ChunkStart = 1;
ChunkEnd = SegmentLength;
sigblock = [];
for t = 1:samples/SegmentLength
    sigblock = sig(ChunkStart:ChunkEnd);
    blockfft = fft(sigblock,SegmentLength);
    out_sigg = real(ifft(blockfft.*filterffte'));
    out_sig = [out_sig out_sigg];

    ChunkStart = ChunkEnd;
    ChunkEnd = ChunkEnd + SegmentLength -1 ;
  • $\begingroup$ Read up on "overlap add" en.wikipedia.org/wiki/Overlap%E2%80%93add_method. You can also just use the built in Matlab function fftfilt() that implements it $\endgroup$
    – Hilmar
    Jul 22 '19 at 13:19
  • $\begingroup$ Added following code but getting same problem DesignedFilt = designfilt('lowpassfir', 'FilterOrder', 32,'CutoffFrequency', 20, 'SampleRate', Fs); out_sigg = fftfilt(DesignedFilt,sigblock,SegmentLength); $\endgroup$
    – Ans Hafeez
    Jul 22 '19 at 13:37

The sharp peaks are actually due to segmentation and concatenation, you need to have overlapping segmentations. The peaks occur on the edges of segments mostly. In following figures I tried to show what I mean, blue curve are Hanning function coefficients. I did not understood why your application is so, but if it is essential to process your signal in segmented mode, you should consider overlapped segments. For your case each segment of signal must have 25% overlap with the next one, in other words, assuming the segment length to be $N$, if $x_{i-1}(t)$ is a segment of the signal you must have $x_i(t)$ where $x_{i-1}(0.75N : N) = x_i(0:0.25 N)$.

enter image description here

The following shows a 50% overlapping mode,

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.