How do I plot this convolution correctly???
조회 수: 5 (최근 30일)
이전 댓글 표시
I am trying to plot the following function sinc(x/.2) convolved with rect(x/10)
clc; close all; clear all;
x = linspace(-10,10,1001);
grid
hold;
plot(x,sinc(x/.2)),'r';
plot(x,rect(x/10)),'c';
y=conv(sinc(x/.2),rect(x/10),'full');
axis=(0:length(y)-1)*(x(2)-x(1));
plot(axis,y);
The resultant plot I am getting is shifted to 15. Shouldnt it be between -5 to 5? Thank you
채택된 답변
Image Analyst
2012년 11월 2일
No, because the convolved signal is the length of the original signal plus the length of the filter. And you had the values for the new x axis wrong. Try this:
clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 16;
numberOfElements = 1001;
middleElement = ceil(numberOfElements/2);
x = linspace(-10,10, numberOfElements);
halfWindowWidth = 150;
% Make rectangular pulse.
rect = zeros(1, numberOfElements);
rect(middleElement-halfWindowWidth: middleElement+halfWindowWidth) = 1;
subplot(3, 1, 1);
plot(x, rect,'b-', 'LineWidth', 2);
ylim([0 2]);
grid 'on';
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Make sinc.
subplot(3, 1, 2);
s = sinc(x/2);
plot(x, s, 'b-', 'LineWidth', 2);
grid 'on';
% Convolve
y = conv(s, rect,'full');
predictedLength = length(s)+length(rect)-1
newXAxis = linspace((-10 - halfWindowWidth), (+10 + halfWindowWidth), length(y)) ;
subplot(3, 1, 3);
plot(newXAxis, y, 'b-', 'LineWidth', 2);
grid 'on';
댓글 수: 2
Image Analyst
2012년 11월 2일
I'm not sure what you're asking. I already showed you. You need to look at my code. You can't just pull in one of my lines and then modify it like this:
axis = linspace((-10 - halfWindowWidth), (+10 + halfWindowWidth), length(y)) ;
First of all, I defined halfWindowWidth and you did not copy over that part, so of course it's undefined for you. Secondly I carefully chose my variable names, so I used newXAxis instead of axis. You used axis, which just blew away the built in axis() function - NOT a good idea.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!