How to perform forward and backward lowpass filtering with filter command?

36 ビュー (過去 30 日間)
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota 2023 年 11 月 2 日
回答済み: Sanju 2023 年 12 月 15 日
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 日
編集済み: 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?

サインインしてコメントする。

回答 (1 件)

Sanju
Sanju 2023 年 12 月 15 日
I understand that you want to perform backward filtering using "filter" command,
To perform backward filtering with the “filter” command, you can use the same approach as for forward filtering, but with the reversed input and output signals.
Here's a code snippet you can refer to,
% Perform backward filtering with filter command
q1_rev = flipud(q1);
AfiltFilter_rev = filter(h, 1, q1_rev.*exp(-1j*flipud(phi)));
AfiltFilter_rev = flipud(AfiltFilter_rev);
Regarding “filtfilt” command, yes, it is possible to perform the same operation with the “filtfilt” command. The “filtfilt” function performs zero-phase filtering, which means that it applies the filter twice, once forward and once backward, to eliminate any phase distortion.
Here's a code snippet you can refer to,
% Perform zero-phase filtering with filtfilt command
Afiltfiltfilt_rev = filtfilt(h, 1, q1_rev.*exp(-1j*flipud(phi)));
Afiltfiltfilt_rev = flipud(Afiltfiltfilt_rev);
Regarding the triangular conversion in the “filtfilt” output, this is since the “filtfilt” function applies the filter twice, once forward and once backward, which effectively doubles the filter order. This can cause the frequency response of the filter to become more triangular, especially if the filter has a sharp cutoff. However, this should not affect the magnitude response of the filter, which should still be a lowpass filter with a cutoff frequency of fc/(fs/2).
Note: In both cases, we need to reverse the input signal and the phase vector before applying the filter, and then reverse the output signal again to obtain the desired result.
You can also refer to the below document on “filtfilt” if required,
Hope this Helps!
Thanks.

カテゴリ

Help Center および File ExchangeMatched Filter and Ambiguity Function についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by