How to use a designed filter to convolve a signal.

30 views (last 30 days)
I designed a filter using the filter designer but I don't know how to use the exported code. I want to use the coefficients produced from the filter to convolve the signal and the filter together (without exporting and loading the coefficients).
Sample Designed filter:
function Hd = lowpassfilter_0_10
%LOWPASSFILTER_0_10 Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.1 and the DSP System Toolbox 9.3.
% Generated on: 24-Apr-2017 10:42:14
% Equiripple Lowpass filter designed using the FIRPM function.
% All frequency values are in Hz.
Fs = 204; % Sampling Frequency
Fpass = 0; % Passband Frequency
Fstop = 10; % Stopband Frequency
Dpass = 0.057501127785; % Passband Ripple
Dstop = 0.0001; % Stopband Attenuation
dens = 20; % Density Factor
% Calculate the order from the parameters using FIRPMORD.
[N, Fo, Ao, W] = firpmord([Fpass, Fstop]/(Fs/2), [1 0], [Dpass, Dstop]);
% Calculate the coefficients using the FIRPM function.
b = firpm(N, Fo, Ao, W, {dens});
Hd = dfilt.dffir(b);
How do I use this function within a code to convolve a signal and this filter together? Do I have to do something like:
filtered_signal = conv(signal, Hd);
*To explain the process further: Right now I'm just designing the filter in filter designer, exporting the coefficients into an .mat file, and getting the filtered signal using the convolution.
signal = C{1,1}{1,2}; %my data is in C{1,1}{1,2}
uiopen('matlab','Select filter'); %getting the exported coefficients (variable name lowpass)
fsignal = conv(signal,lowpass); %convolving the signal and filter
Is there a better way to do this without exporting and importing into the workspace and just using the exported code above?

Accepted Answer

Honglei Chen
Honglei Chen on 24 Apr 2017
You may want to use
filtered_signal = filter(Hd,signal);
filter and conv is essentially the same except that filter keeps the output the same size as input and save extra samples in the state for the signal in the next frame. If you really want to use conv you can do
filtered_signal = conv(signal, Hd.Numerator);
HTH
  4 Comments
Jason Jeong
Jason Jeong on 25 Apr 2017
Edited: Jason Jeong on 25 Apr 2017
This is the weird image I get. You can pretty easily see that the filtered signal looks shifted to the right.
As for the quick test, this is what I got:
>> H = dfilt.dffir(rand(1,5))
H =
FilterStructure: 'Direct-Form FIR'
Arithmetic: 'double'
Numerator: [0.0975404049994095 0.278498218867048 0.546881519204984 0.957506835434298 0.964888535199277]
PersistentMemory: false
>> filter(H,ones(1,10))
ans =
0.0975 0.3760 0.9229 1.8804 2.8453 2.8453 2.8453 2.8453 2.8453 2.8453
>> conv(H.Numerator,ones(1,10))
ans =
Columns 1 through 11
0.0975 0.3760 0.9229 1.8804 2.8453 2.8453 2.8453 2.8453 2.8453 2.8453 2.7478
Columns 12 through 14
2.4693 1.9224 0.9649
I did some further looking into it, just trying the convolution (gave me 1075x1) and plotting. I see that the convolution (blue) is the same as the filtered one (red) so it seems like the filtering just cuts off the last 51 data points to get the filtered signal (1024x1) without centering it.
Honglei Chen
Honglei Chen on 25 Apr 2017
Yes, that is correct. I think I now understand what you mean by the delay. That is not the difference between filter and conv command. Rather it is the group delay introduced by the filtering operation itself.
HTH

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!