Help IFFT can not restore to original signal after filtering
8 views (last 30 days)
Show older comments
I tried to compare original signal and signal after high pass filter using ifft. but the result seems odd to me. Can anyone explain why its happened
here is my code
%USING REAL SIGNAL
data_full=load("sig_tremor.txt");
data=data_full(:,2);
srate=100;
t=data_full(:,1);
n=length(t);
figure(1)
subplot(211), plot(t,data), hold on
% HIGH PASS FILTER (SIGNAL*KERNEL)
X=fft(data);
hz=linspace(0,srate,n);
subplot(212), plot(hz,2*abs(X)/n,'-b')
hold on, set(gca,'xlim',[0 10])
% applying high pass filter --> multiplied by kernel
% hz+10 indicate how high freq can be passed
filterkernel = (1./(1+exp(-hz+1)));
XHP=X.*filterkernel;
subplot(211), plot(t,real(ifft(XHP)),'r')
xlabel('Time'), ylabel('Amplitude')
legend('original','HP filter')
subplot(212), plot(hz,2*abs(XHP)/n,'r')
set(gca,'xlim',[0 10],'xtick',[0:2:25])
xlabel('Frequency')
legend('original','HP filter')
hold off
0 Comments
Accepted Answer
Star Strider
on 28 Aug 2023
The fftfilt function can do this relatively efficiently, however since you want to do it manually, try this —
%USING REAL SIGNAL
data_full=load("sig_tremor.txt");
data=data_full(:,2);
% srate=100;
t=data_full(:,1);
srate = 1/mean(diff(t));
n=length(t);
figure(1)
subplot(211), plot(t,data), hold on
% HIGH PASS FILTER (SIGNAL*KERNEL)
X=fft(data);
size(X)
hz=linspace(0,srate,n);
hz2=linspace(0,(n/2)-1,fix(n/2))*2/srate;
subplot(212), plot(hz,2*abs(X(1:numel(hz)))/n,'-b')
hold on, set(gca,'xlim',[0 10])
% applying high pass filter --> multiplied by kernel
% hz+10 indicate how high freq can be passed
filterkernel = (1./(1+exp(-(hz(1:fix(n/2))+1)))).';
figure
plot(filterkernel)
grid
xlim([0 100])
title('Filter passband')
Xs = fftshift(X);
n2 = fix(n/2)
XHP = [Xs(1:n2).*flip(filterkernel); Xs(n2+1:end).*filterkernel];
XHPs = ifftshift(XHP);
XHPi = ifft(XHPs);
Fs = srate;
Fv = linspace(-Fs/2, Fs/2-Fs/length(X), length(X)); % EVEN 'length(X)' (Asymmetric)
% XHP=X.*filterkernel;
figure
subplot(211), plot(t,real(ifft(XHPs)),'r')
xlabel('Time'), ylabel('Amplitude')
title('original')
subplot(212), plot(hz,2*abs(XHPs)/n,'r')
set(gca,'xlim',[0 10],'xtick',[0:2:25])
xlabel('Frequency')
title('HP filter')
hold off
figure
plot(t-t(1),real(XHPi),'r', 'DisplayName','HP filter')
hold on
plot(t-t(1), data, 'b', 'DisplayName','Original')
hold off
set(gca,'xlim',[0 10],'xtick',[0:2:25])
xlabel('Time')
hold off
legend('Location','best')
It was difficult to figure you what you are doing here, so I had to make some changes.
The filter itself actually does not do much to the signal.
.
2 Comments
Star Strider
on 28 Aug 2023
My pleasure!
If my Answer helped you solve your problem, please Accept it!
If you want to use filtfilt with the filter you created, first use a vector created from it (such as I created in the second figure here, with an associated frequency vector and the requested filter order) it as inputs to the firls function. That will create a FIR filter that should closely match it.
.
More Answers (0)
See Also
Categories
Find more on Filter Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!