convolution of two function
20 ビュー (過去 30 日間)
古いコメントを表示
I have two functions as below:
t = 0 : pi/180 : pi;
f = exp(-t/3) .* (sin(2*t) + 2*sin(4*t) + 0.4*sin(2*t).*sin(40*t));
h = 10 * exp(-10*t);
I want to calcluate the convolution of f and h (L = f * h (t)). This means that to filter signal f using h and store results in L. In fact, this is the example 2.21 of "A first course in wavelets with Fourier analysis".
Plot of f is:
![f.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270671/f.jpeg)
Plot of L, that is the filtered signla is:
![L.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270672/L.jpeg)
And this is what MATLAB calculates using conv function
G = conv(f,h,'same');
![G.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270673/G.jpeg)
What is going wrong? G should be same as L as depicted in Figure 15.
0 件のコメント
回答 (2 件)
sumanth chinna
2020 年 10 月 8 日
clc
clear all
close all
x=[1 1 1 1 -1 -1 -1 -1]
h=[0 1 2 3 4 3 2 1]
l1=length(x)
l2=length(h)
N=max(l1,l2)
cconv(x,h,N)
disp('without using standard function')
x=[x zeros(1,N-l1)]
h=[h zeros(1,N-l2)]
y=zeros(1,N)
for n=1:N
for m=1:N
j=mod(n-m,N)
j=j+1
y(n)=y(n)+x(m)*h(j)
end
end
1 件のコメント
AKASH KUMAR
2022 年 1 月 27 日
編集済み: AKASH KUMAR
2022 年 1 月 27 日
%%%%%
% algorithm to compute convolution
clc
clear
close all
x1 = [5,6,1];
h = [10,6,4,8,9,5];
N = length(x1)+length(h)-1;
y=conv(x1,h) % Inbuilt matlab function
x = linearconvolve(x1,h);
x
%% 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
Image Analyst
2020 年 2 月 9 日
Looks like it's an edge effect where zeros outside the signal are getting convolved in when the signals start to not overlap anymore. Instead of 'same', use 'full' to see the full signal.
2 件のコメント
Image Analyst
2020 年 2 月 9 日
parham, the plots are correct for the filters you gave. You might want to normalize h so that the filtered signal is of the same amplitude as the original signal:
t = 0 : pi/180 : pi;
f = exp(-t/3) .* (sin(2*t) + 2*sin(4*t) + 0.4*sin(2*t).*sin(40*t));
h = 10 * exp(-10*t);
% Normalize h
h = h / sum(h);
plot(t, f, 'b-', 'LineWidth', 2);
grid on;
hold on;
plot(t, h, 'r-', 'LineWidth', 2);
legend('f', 'h');
filtered_f = conv(f, h, 'full');
deltat = t(2)-t(1)
tf = linspace(-length(h)*deltat, deltat * (length(f) + length(h)), length(filtered_f));
plot(tf, filtered_f, 'g-', 'LineWidth', 2);
legend('f', 'h', 'filtered_f', 'Interpreter', 'none', 'Location', 'northwest');
![0001 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270710/0001%20Screenshot.png)
I think that maybe what you aren't realizing is that the convolution flips the filter signal from right ot left, so that the red signal essentially has the blip on the right end as it slides across the blue signal, not the left side like it's plotted. So then the red blip is way off the right end of the blue signal, only the flat, essentially zero, part of the signal is there to multiply by the blue signal, so the output signal is a constant zero on the right.
You'll notice that there are two positive humps and two negative humps, just like your desired output signal.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!