I wrote a code to design a lowpass filter and to filter an input signal with this filter; but Matlab gave an error. I could not understand my error. How can I correct this error?

12 views (last 30 days)
function [srf]=lowpass(sr,fcut)
Hf=fdesign.lowpass('n,fc',20, fcut);
Hd = design(Hf);
srf = conv(sr,Hd);
end

Answers (1)

Wayne King
Wayne King on 2 Dec 2013
Edited: Wayne King on 2 Dec 2013
It's always best to show the error that you get: "MATLAB gave an error" is not helpful.
Assuming that sr is your signal, the problem is that conv() is not overloaded for dfilt objects.
You do not want to use conv() to filter your signal. You want to use filter()
Change this line:
srf = conv(sr,Hd)
to
srf = filter(Hd,sr);
Also, why not add the sampling frequency as an input to your function. You can then supply that as the final input to fdesign.lowpass() and design your filter in cycles/unit time.
For example:
function [srf]=lowpass(sr,fcut,Fs)
Hf=fdesign.lowpass('n,fc',20, fcut,Fs);
Hd = design(Hf);
srf = filter(Hd,sr);
end

Community Treasure Hunt

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

Start Hunting!