Filter löschen
Filter löschen

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

22 Ansichten (letzte 30 Tage)
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
% 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));
hold on
set(gca,'Fontsize',14); xlabel('s'); ylabel('Amplitude')
xlim([3.5 4.5])
title('filter bandwidth of fc = 10Hz')
  2 Kommentare
Bruno Luong
Bruno Luong am 2 Nov. 2023
Bearbeitet: Bruno Luong am 2 Nov. 2023
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 am 2 Nov. 2023
Bearbeitet: Kalasagarreddi Kottakota am 2 Nov. 2023
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sanju am 15 Dez. 2023
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!

Community Treasure Hunt

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

Start Hunting!

Translated by