How to use the convolution function rather than filter for signal
81 ビュー (過去 30 日間)
古いコメントを表示
Abdulaziz Shaqhan
2020 年 10 月 10 日
コメント済み: Abdulaziz Shaqhan
2020 年 10 月 19 日
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 件のコメント
採用された回答
Chaitanya Mallela
2020 年 10 月 19 日
編集済み: Chaitanya Mallela
2020 年 10 月 19 日
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));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Digital and Analog Filters についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!