How can I plot a data set after applying cascaded filter?

7 visualizaciones (últimos 30 días)
Soeun Lee
Soeun Lee el 19 de Jul. de 2021
Comentada: Mathieu NOE el 19 de Jul. de 2021
I created a cascaded filter and applied it to my datasets. How can I plot the datasets after the filter is applied? I'm getting an error that says "Error using plot. Data must be numeric, datetime, duration or an array convertible to double". Please let me know what I can do to create the plot. Thank you in advance.
Fs=24414;
X=RawData;
Wn=10; % low cutoff
Wn_2=[60 60];
[b1,a1]=butter(5,Wn/Fs,"high");
[b2,a2]=butter(5,Wn_2/Fs,"stop");
H1=dfilt.df2t(b1,a1);
H2=dfilt.df2t(b2,a2);
Hcas=dfilt.cascade(H1,H2)
plot(Hcas)

Respuestas (2)

Mathieu NOE
Mathieu NOE el 19 de Jul. de 2021
hello Soeun
I did a couple of modifications / corrections in your code
please note that for the notch filter , the low and high cut off frequencies cannot be equal , so you have to give a non zero bandwith to your filter
also i reduced the order friom 5 to 2 as the bode plot was not correct in my opinion; I always wondered why the butter function would generate strange filters (sometimes unstable) with high orders. So as far as I can live with it , I stick to N = 2;
I generated some dummy data as I don't have yours
clc
clearvars
Fs=24414;
% X=RawData;
samples = 1e4;
t = (0:samples -1)'/Fs;
X=0.1*randn(samples,1)+sin(2*pi*60*t);
fn=10; % low cutoff
fn_2=[60-5 60+5]; % the low and high freqs must not coincide
[b1,a1]=butter(2,fn*2/Fs,"high");
figure(1),dbode(b1,a1,1/Fs); % debug / check filter is correct
[b2,a2]=butter(2,fn_2*2/Fs,"stop");
figure(2),dbode(b2,a2,1/Fs); % debug / check filter is correct
H1=dfilt.df2t(b1,a1);
H2=dfilt.df2t(b2,a2);
Hcas=dfilt.cascade(H1,H2);
out = filter(Hcas,X);
figure(3),
plot(X)
hold on
plot(out)
hold off
  2 comentarios
Soeun Lee
Soeun Lee el 19 de Jul. de 2021
Thank you for the response. I would like to ask why you multiplied 2 to fn and fn_2 before dividing it with Fs?
Mathieu NOE
Mathieu NOE el 19 de Jul. de 2021
the butter filter needs frequencies being normalized by the Nyquist frequency (= Fs/2) and not the sampling frequency (Fs) . if you plot the Bode plot of the filters , you would have seen the factor 2 was lacking

Iniciar sesión para comentar.


Sanket Sane
Sanket Sane el 19 de Jul. de 2021
Editada: Sanket Sane el 19 de Jul. de 2021
You cannot directly plot a filter object. Convert the filter to its representation in the frequency domain using the freqz function.
[h,w] = freqz(b, a, n)
Where h is the frequency response (vector of complex numbers), w is the normalized angular frequency, b and a are the transfer coefficients you obtained using butter, and n is the number of points.
Or you can use
[h,w] = freqz(d, n)
Where d is the digital filter. Eg. H1 in your case.
Use abs(h) to convert h to magnitude.
Another option is using
fvtool(b,a)
Which plots the frequency response magnitude in dB.
Then to filter your data, you can use.
Y = filter(b, a, X).
Then
plot(Y)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by