필터 지우기
필터 지우기

How do I plot this convolution correctly???

조회 수: 5 (최근 30일)
Poonam
Poonam 2012년 11월 2일
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
Poonam
Poonam 2012년 11월 2일
Poonam
Poonam 2012년 11월 2일
function out = pulse(in);
out = zeros(size(in)); out = step(in + 1/2) - step(in - 1/2);
This is my rect function

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
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
Poonam
Poonam 2012년 11월 2일
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 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개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by