필터 지우기
필터 지우기

How to filter signals properly?

조회 수: 3 (최근 30일)
Hasan Kaan Tuna
Hasan Kaan Tuna 2023년 7월 31일
댓글: Star Strider 2023년 7월 31일
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
Walter Roberson
Walter Roberson 2023년 7월 31일
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 2023년 7월 31일
Just define x with the values inside the sampleSignal.mat...

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 7월 31일
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
Hasan Kaan Tuna
Hasan Kaan Tuna 2023년 7월 31일
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 2023년 7월 31일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Single-Rate Filters에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by