How to filter signals properly?

2 visualizaciones (últimos 30 días)
Hasan Kaan Tuna
Hasan Kaan Tuna el 31 de Jul. de 2023
Comentada: Star Strider el 31 de Jul. de 2023
Hi,
I have a .mat file which is given in the attachments. I'd like to apply low pass filter to it. So, I might be able to analyse it properly. For this purpose, I'd like to use designfilt() function. How can I properly construct a .m file to filter my signal?
Ts = 0.005; % Sampling Interval (s)
Fs = 1/Ts; % Sampling Frequency (Hz)
%d=designfilt('lowpassfir', 'FilterOrder', 100, 'CutoffFrequency', 20, 'SampleRate', Fs);
d=designfilt('lowpassfir','PassbandFrequency',0.25,'StopbandFrequency',0.35,'PassbandRipple',0.5,'StopbandAttenuation',65,'DesignMethod','kaiserwin');
y=filter(d,sampleSignal);
plot(x,'--');
hold on
plot(y)
Thank you.
  4 comentarios
Walter Roberson
Walter Roberson el 31 de Jul. de 2023
whos -file sampleSignal
Name Size Bytes Class Attributes sampleSignal 387196x1 3097568 double
There is no x inside that .mat file.
Hasan Kaan Tuna
Hasan Kaan Tuna el 31 de Jul. de 2023
Just define x with the values inside the sampleSignal.mat...

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 31 de Jul. de 2023
The filter is not going to do much actual filtering.
Try this —
LD = load('sampleSignal.mat');
sampleSignal = LD.sampleSignal;
L = size(sampleSignal,1);
Ts = 0.005; % Sampling Interval (s)
Fs = 1/Ts; % Sampling Frequency (Hz)
t = linspace(0, L-1, L)/Fs; % Time Vector
%d=designfilt('lowpassfir', 'FilterOrder', 100, 'CutoffFrequency', 20, 'SampleRate', Fs);
d=designfilt('lowpassfir','PassbandFrequency',0.25,'StopbandFrequency',0.35,'PassbandRipple',0.5,'StopbandAttenuation',65,'DesignMethod','kaiserwin');
y=filtfilt(d,sampleSignal);
lpfirc = d.Coefficients;
figure
freqz(lpfirc, 1, 2^16, Fs)
[FTs1,Fv] = FFT1(sampleSignal,t);
figure
plot(Fv, abs(FTs1)*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
title('Fourier Transform Of ‘sampleSignal’')
xline(0.25*Fs/2, '--g', 'Passband Frequency')
xline(0.35*Fs/2, '--r', 'Stopband Frequency')
% xlim([0 50])
figure
plot(t, sampleSignal,'--', 'LineWidth',1)
grid
hold on
plot(t, y)
hold off
function [FTs1,Fv] = FFT1(s,t)
s = s(:);
t = t(:);
L = numel(t);
Fs = 1/mean(diff(t));
Fn = Fs/2;
NFFT = 2^nextpow2(L);
FTs = fft((s - mean(s)).*hann(L), NFFT)/sum(hann(L));
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
FTs1 = FTs(Iv);
end
.
  2 comentarios
Hasan Kaan Tuna
Hasan Kaan Tuna el 31 de Jul. de 2023
Thank you for your answer. Could you please briefly explain your code? I am bit confused about passband and stopband freqs.
Star Strider
Star Strider el 31 de Jul. de 2023
As always, my pleasure!
Sure!
The first part loads the file and creates a time vector using the available information.
The next part uses your designfilt call to design the filter and then specifically uses filtfilt rather than filter to do the actual filtering. I extracted the FIR filter denominator coefficients from ‘d’ to display the filter Bode plot using the freqz function.
I want to see what the signal spectrum looks like, so I used my ‘FFT1’ function to calculate the Fourier transform, and then plotted it, also with respect to the filter passband and stopband frequencies displayed using the xline calls.
The last plot is your plot of the filtered and unfiltered signals, slightly changed to make the unfiltered signal a bit easier to see.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by