How to perform forward and backward lowpass filtering with filter command?
20 次查看(过去 30 天)
显示 更早的评论
I need to perform forward and backward filtering with filter commnad in matlab. I was able to perform forward filtering, but not backward. In this regard, could you help me with the following code?
- I used convolution with rectangular window for lowpass filtering
- In second case I used filter matlab comand with boxcar for lowpass filtering
- In case three, I used filtfilt with boxcar for lowpass filetering
The first case with conv is correct and I have analytically validated. But other two approaches have issues, so how to correct them? *Please dont chnage the filter type,as I need only boxcar.
Needs: 1. Using filter creates a forward delay. How to perform backward filtering with filter command?
2. Is it possible to perform same operation with filtfilt matlab command?
clear all; close all; clc;
fs=8192*2; % sampling frequency
dt = 1/fs; % sample time
T=8; % duration of the signal
Nt = T*fs; % total number of samples
t = 0:dt:T-dt; % time vector
f0 = 500;
fT = 4000;
finst = linspace(f0,fT, length(t)).';
phi = 2*pi*cumsum(finst)*dt;
fc = 10; % cutoff
N=round(fs/fc);
% source 1
a1 = 1;
b1 = 6000;
c1 = 2;
c2 = 1.5;
A1 = ((a1 * exp(-b1 * (t - T/c1).^2 + 1j)));
q1 = A1.'.*exp(1j*phi);
%% using convolution for lowpass fltering
Afiltconv = conv(q1.*exp(-1j*phi),ones(1,N)./N, 'same');
%% using filter command for filtering
fc = fc/(fs);
N = 1/fc;
h = boxcar(N);
h = h/sum(h);
AfiltFilter = filter(h, 1, q1.*exp(-1j*phi));
%% using filtfilt command
Afiltfiltfilt = filtfilt(h, 1, q1.*exp(-1j*phi));
figure()
plot(t,abs(A1),'LineWidth',2)
hold on
plot(t,abs(AfiltFilter),'--k','LineWidth',2)
plot(t,abs(Afiltfiltfilt),'r','LineWidth',2)
plot(t,abs(Afiltconv),'LineWidth',2)
legend('True','filter','filtfilt','conv')
set(gca,'Fontsize',14); xlabel('s'); ylabel('Amplitude')
xlim([3.5 4.5])
title('filter bandwidth of fc = 10Hz')
2 个评论
Bruno Luong
2023-11-2
编辑:Bruno Luong
2023-11-2
You have not explained why it doesn't meet your expectation.
Filter is causal so it has delay. filtfilt squares the amplitude-response and has zero-phase delay, in case of rectanguar input it becomes triangular kernel convolution.
Your plots show exactly that.
I seems that you expect something that these commands do not suppose to do, but you not expressed it.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Digital Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!