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?
  1. I used convolution with rectangular window for lowpass filtering
  2. In second case I used filter matlab comand with boxcar for lowpass filtering
  3. 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);
Warning: Rounding order to nearest integer.
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
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.
Kalasagarreddi Kottakota
编辑:Kalasagarreddi Kottakota 2023-11-2
Regarding just forward filtering, I know why it is delayed in time, which is reflected in the simulation.
Second, I had the doubt in filtfilt (forward and backward filtering), when I had rectangular input, the output is traingular. This is where I was bit confused. Thanks for your answer, but can you explain little bit more about this traingular conversion?
As you said, that filtfilt squares the amplitude, then the magnitude result, which is shown in the plot by filtfilt is wrong? Is it fc = fc/(fs); or fc = fc/(fs/2)?
Can you help me with backward filtering using filter command?

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by