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

12 views (last 30 days)
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.
Wn=10; % low cutoff
Wn_2=[60 60];

Answers (2)

Mathieu NOE
Mathieu NOE on 19 Jul 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
% X=RawData;
samples = 1e4;
t = (0:samples -1)'/Fs;
fn=10; % low cutoff
fn_2=[60-5 60+5]; % the low and high freqs must not coincide
figure(1),dbode(b1,a1,1/Fs); % debug / check filter is correct
figure(2),dbode(b2,a2,1/Fs); % debug / check filter is correct
out = filter(Hcas,X);
hold on
hold off
Soeun Lee
Soeun Lee on 19 Jul 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 on 19 Jul 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

Sign in to comment.

Sanket Sane
Sanket Sane on 19 Jul 2021
Edited: Sanket Sane on 19 Jul 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
Which plots the frequency response magnitude in dB.
Then to filter your data, you can use.
Y = filter(b, a, X).


Community Treasure Hunt

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

Start Hunting!