Error using stem()...... X must be same length as Y.
조회 수: 16 (최근 30일)
이전 댓글 표시
clc;
clear all;
close all;
i=-5:0.1:5;
l=length(i);
%unit step signal
for t=1:1
if i(t)<0
u(t)=0;
else
u(t)=1;
end;
end;
figure(1)
subplot(2,1,1);
plot(i,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(i,u);
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
댓글 수: 5
KSSV
2023년 9월 20일
This is the whole code given by the OP:
clc; clear ;
clc; clear all; close all;
i=-5:0.1:5;
l=length(i);
%unit step signal
for t=1:1
if i(t)<0
u(t)=0;
else
u(t)=1;
end;
end;
figure(1)
subplot(2,1,1);
plot(i,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(i,u(t));
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
% ramp signal
for t=1:1
if i(t)<0
r(t)=0;
else
r(t)=i(t);
end;
end;
figure(2)
subplot(2,1,1);
plot(i,r);
xlabel('time');
ylabel('amplitude');
title('continous time ramp signal');
subplot(2,1,2);
stem(i,r);
xlabel('time');
ylabel('amplitude');
title('disc time unit ramp signal');
%sinusoidal signal
f=0.1;
sinus=sin(2*pi*f*i);
figure(3)
subplot(2,1,1);
plot(i,sinus);
xlabel('time');
ylabel('amplitude');
title('continous time sinusoidal signal');
subplot(2,1,2);
stem(i,sinus);
xlabel('time');
ylabel('amplitude');
title('discrete time sinusoidal signal');
%impulse signal
for t=1:1
if i(t)==0
imp(t)=1;
else
imp(t)=0;
end;
end;
figure(4)
subplot(2,1,1);
plot(i,imp);
xlabel('time');
ylabel('amplitude');
title('continous time impulse signal');
subplot(2,1,2);
stem(i,imp) ;
xlabel('time');
ylabel('amplitude');
title('discrete time impulse signal');
% exponential signal
n=-1:0.1:1;
a=1;
exp11=exp(a*n);
figure(5)
subplot(2,1,1);
plot(n,exp11);
xlabel('time');
ylabel('amplitude');
title('continous time exponential signal');
subplot(2,1,2);
stem(n,exp11);
xlabel('time');
ylabel('amplitude');
title('discrete time exponential signal');
%traingular signal
t=0:0.01:1;
d=0:0.2:1;
y1=pulstran(t,d,'tripuls',0.1);
d=0.1:0.2:1;
y2=pulstran(t,d,'tripuls',0.1);
tri=y2-y1;
figure(6)
subplot(2,1,1);
plot(i,tri);
xlabel('time');
ylabel('amplitude');
title('continous time triangular signal');
subplot(2,1,2);
stem(i,tri);
xlabel('time');
ylabel('amplitude');
title('discrete time triangular signal');
% square signal
i=0:0.1:3;
l=length(i);
f=1;
sq=square(2*pi*f*i);
figure(7)
subplot(2,1,1);
plot(i,sq);
xlabel('t--->');
ylabel('amp --->');
title('continous time square signal');
subplot(2,1,2);
stem(i,sq); xlabel('samples --->');
ylabel('amplitude --->');
title('discrete time square signal');
% sinc function
i=-5:0.1:5;
l=length(i);
ss=sinc(i);
figure(8)
subplot(2,1,1);
plot(i,ss);
xlabel('t--->');
ylabel('amp --->');
title('continous time sinc signal');
subplot(2,1,2);
stem(i,ss);
xlabel('samples --->');
ylabel('amplitude --->');
title('discrete time sincsignal');
% sawtooth waveform
T=10*(1/50);
fs=1000;
dt=1/fs;
t=0:dt:T-dt;
y=2*sawtooth(2*pi*50*t);
figure(1);
subplot(2,1,1);
plot(t,y);
grid on;
subplot(2,1,2);
stem(t,y);
grid on;
채택된 답변
추가 답변 (1개)
KSSV
2023년 9월 20일
I have given one wproper working code for unit step signal. Understand it and change the others like wise.
t=-5:0.1:5;
nt = length(t) ;
%unit step signal
u = zeros(1,nt) ;
for i=1:nt
if t(i) < 0
u(i)=0;
else
u(i)=1;
end
end
figure(1)
subplot(2,1,1);
plot(t,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(t,u);
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
You need not to use aloop. Without loop:
t=-5:0.1:5;
nt = length(t) ;
%unit step signal
u = ones(1,nt) ;
u(t<0) = 0 ;
figure(1)
subplot(2,1,1);
plot(t,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(t,u);
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!