How do I plot this convolution correctly???

3 views (last 30 days)
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
  3 Comments
Poonam
Poonam on 2 Nov 2012
function out = pulse(in);
out = zeros(size(in)); out = step(in + 1/2) - step(in - 1/2);
This is my rect function

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 2 Nov 2012
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 Comments
Poonam
Poonam on 2 Nov 2012
clc; close all; clear all;
x = linspace(-10,10,1001);
grid
hold;
a = sinc(x/.2)
plot(x,a),'r';
b = rect(x/10)
plot(x,b),'c';
y=conv(a,b,'full');
axis = linspace((-10 - halfWindowWidth), (+10 + halfWindowWidth), length(y)) ;
%axis=(0:length(y)-1)*(x(2)-x(1));
plot(axis,y);
This is my rect function
function out = pulse(in);
out = zeros(size(in)); out = step(in + 1/2) - step(in - 1/2);
How do I change the axis to suit my code? Thank you so much for your help :)
Image Analyst
Image Analyst on 2 Nov 2012
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!