フィルターのクリア

Manual Code for convolution

297 ビュー (過去 30 日間)
Kwanghun Choi
Kwanghun Choi 2017 年 11 月 6 日
コメント済み: AKASH KUMAR 2024 年 8 月 17 日 9:53
Hello there!! Now, I make a code for convolution. data is discrete like
x=[1,3,5,7,9]; y=[5,4,3,2,1];
It mean if t=1, x is 1 and y=5 / if t=3, x is 5, y is 3 ....
So I cannot use 'conv' function. My code is like that
for t=1:5
convolution(t)=0;
for j=1:t
convolution(t)=convolution(t)+x(t+1-j)*y(j);
end
end
But result is not good. Are there something to fix?

採用された回答

Birdman
Birdman 2017 年 11 月 6 日
x=input('Enter x: ')
h=input('Enter h: ')
m=length(x);
n=length(h);
X=[x,zeros(1,n)];
H=[h,zeros(1,m)];
for i=1:n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
else
end
end
end
Y

その他の回答 (2 件)

AKASH KUMAR
AKASH KUMAR 2022 年 2 月 8 日
%
% algorithm to compute convolution
clc
clear
close all
x1 = [5,6,1,2];
h = [10,6,4,8,9,5];
N = length(x1)+length(h)-1;
y=conv(x1,h) % Inbuilt matlab function
y = 1×9
50 90 66 90 109 95 55 23 10
x = linearconvolve(x1,h); % Function call
x
x = 1×9
50 90 66 90 109 95 55 23 10
%% User defined function to find linear convolution
function cnv = linearconvolve(a,b)
L = length(a)+length(b)-1;
cnv = zeros(1,L);
a1=[a,zeros(1,L-length(a))]; % define a new vector of a
b1=[b,zeros(1,L-length(b))];
for i=1:L
c = 0;
for j=1:i
c = c + a1(j)*b1(i-j+1);
end
cnv(i) = c;
end
end
  1 件のコメント
AKASH KUMAR
AKASH KUMAR 2024 年 8 月 17 日 9:53
%
% algorithm to compute convolution
% Note : all signals are stored in a row vector
clc
clear
close all
x1 = [5,6,1,9,1,6,3,6.2,2];
h = [10,6,4];
N = length(x1)+length(h)-1;
y=conv(x1,h) % Inbuilt matlab function
y = 1x11
50.0000 90.0000 66.0000 120.0000 68.0000 102.0000 70.0000 104.0000 69.2000 36.8000 8.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y_filt=filter(h,1,x1)
y_filt = 1x9
50.0000 90.0000 66.0000 120.0000 68.0000 102.0000 70.0000 104.0000 69.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Y1 = linearconvolve1(x1,h) % Function call
Y1 = 1x11
50.0000 90.0000 66.0000 120.0000 68.0000 102.0000 70.0000 104.0000 69.2000 36.8000 8.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Y2= Fir_filter_userDefine(x1,h) % Function call
Y2 = 1x9
50.0000 90.0000 66.0000 120.0000 68.0000 102.0000 70.0000 104.0000 69.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure(1)
stem(y,'r','LineWidth',3);
hold on
stem(y_filt,'b','LineWidth',1.2);
hold on;
stem(Y1,'g')
stem(Y2,'--m');
legend('conv1','filter','conv2','filter2')
%% User defined function to find linear convolution
function cnv = linearconvolve1(a,b)
L = length(a)+length(b)-1;
cnv = zeros(1,L);
a1=[a,zeros(1,L-length(a))]; % define a new vector of a
b1=[b,zeros(1,L-length(b))];
for i=1:L
c = 0;
for j=1:i
c = c + a1(j)*b1(i-j+1);
end
cnv(i) = c;
end
end
% Output length is same as of Input signal
function cnv = Fir_filter_userDefine(X_sig,h_fir)
N=length(X_sig);
L_filt=length(h_fir);
cnv = zeros(1,N);
X_tap=zeros(1,L_filt); % initialize a vector to store incoming signal
for n=1:N
x_n=X_sig(n); % Step 01: Invert the signal
X_tap=[x_n,X_tap(1:L_filt-1)]; % Step 02 : sliding operation.
y_n = h_fir*X_tap'; % Step 03 Summation
% y_n = sum(h_fir.*X_tap); % Step 03 Summation
cnv(n) = y_n;
end
end

サインインしてコメントする。


keshav kumar
keshav kumar 2021 年 8 月 17 日
close all
clear all
x=input('Enter x: ')
h=input('Enter h: ')
m=length(x);
n=length(h);
X=[x,zeros(1,n)];
H=[h,zeros(1,m)];
for i=1:n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
else
end
end
end
Y
stem(Y);
ylabel('Y[n]');
xlabel('----->n');
title('Convolution of Two Signals without conv function');

カテゴリ

Help Center および File Exchange푸리에 분석과 필터링 についてさらに検索

Community Treasure Hunt

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

Start Hunting!