Help IFFT can not restore to original signal after filtering

조회 수: 8 (최근 30일)
nirwana
nirwana 2023년 8월 28일
댓글: Star Strider 2023년 8월 28일
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

채택된 답변

Star Strider
Star Strider 2023년 8월 28일
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)
ans = 1×2
1600 1
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)
n2 = 800
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
nirwana
nirwana 2023년 8월 28일
편집: nirwana 2023년 8월 28일
thanks a lot for nice answer. while waiting, i endup find solution of my code, that is I put transpose kernel filter. But still your code explain better.
NB : i just expolore how to write filter manually as part of training from book, i'll try to use filtfilt as u'r suggestion
Star Strider
Star Strider 2023년 8월 28일
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.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Filter Design에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by