MATLAB Answers

How to use the convolution function rather than filter for signal

44 views (last 30 days)
Hi, I would like to ask how I can apply the convolution for the following code, rather than applying the filter function (note: for Fourier Transform)
Could please anyone help me.
B=[0.5 0.5]; % coefficient of the x(n), x(n-1)
A= [1 -1 0.5];% coefficient of the y(n), y(n-1), y(n-2)
fs=10000;
n=0:1000;
f1=100;
f2=3000;
x=2*sin(2*pi*f1/fs*n)+4*sin(2*pi*f2/fs*n); % input signal
y=filter(B,A,x);% filter
nfft_of_fft = length(y);% number of samples
yf1 = fft(y,nfft_of_fft);%fourier transform function
yf1_mag = abs(yf1);% to make the y-axis more than 0 (absolute value)
yf1_mag_norm = yf1_mag/max(yf1_mag);%normlized the magnitude
yf1_xaxis=fs/2*(0:nfft_of_fft/2-1)/(nfft_of_fft/2);%frequency
plot(yf1_xaxis,yf1_mag_norm(1:nfft_of_fft/2),'r');% plot DEFT
title('Fourier Transform y(n)')
xlabel('Frequency (Hz)')
ylabel('Magnitude')

  0 Comments

Sign in to comment.

Accepted Answer

Chaitanya Mallela
Chaitanya Mallela on 19 Oct 2020
Edited: Chaitanya Mallela on 19 Oct 2020
filter function gives the filter response for both IIR and FIR systems whereas conv function performs linear convolution of the input signals. For FIR filters the functions might perform similar operation but for IIR filters the outputs from both the functions are different.
The output length of filter function is equal to input vector length whereas conv function output length equal to length(filter_vector) + length(input_vector) - 1.
Instead replacement to filter function with conv function in the code can be considered by taking symbolic coeffients
syms z
F = (0.5 + 0.5*z^(-1))/(1-z^(-1)+0.5*z^(-2)); % Symbolic Function
h_sym = iztrans(F); % Inverse z transform
h = double(subs(h_sym,n)); % filter coefficients
y_conv = conv(h,x); % convolution
y_out = y_conv(1:length(x));

More Answers (0)

Community Treasure Hunt

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

Start Hunting!