2014-12-28 66 views
0

我試圖實現使用下面提到的代碼是,在一個繪圖中繪製四個信號x1, x2, x3, x4分別與t1, t2, t3, t4分別。因此,每一秒的第四個不同頻率的新信號被繪製。但是,當我運行代碼時,該圖只顯示空白圖。繪圖功能不顯示任何圖

請讓我知道我在代碼中缺少的東西。

代碼

% Time specifications: 
Fs = 8000;      % samples per second 
dt = 1/Fs;      % seconds per sample 
StopTime = 1;     % seconds 
t = (0:dt:StopTime);   % seconds 

t1 = (0:dt:.25); 
t2 = (.25:dt:.50); 
t3 = (.5:dt:.75); 
t4 = (.75:dt:1); 

x1 = (10)*cos(2*pi*3*t1); 
x2 = (20)*cos(2*pi*6*t2); 
x3 = (30)*cos(2*pi*10*t3); 
x4 = (50)*cos(2*pi*15*t4); 

% Plot the signal versus time: 
figure; 
xlabel('time (in seconds)'); 
ylabel('Amplitude'); 
title('Signal versus Time'); 
plot(t,x1,'r'); 
plot(t,x2,'g'); 
plot(t,x3,'b'); 
plot(t,x4,'black'); 

回答

1

將最後四行

hold on %// this prevents each subsequent `plot` from removing the previous graph 
plot(t1,x1,'r'); %// use appropriate "t" vector: `t1` in this case 
plot(t2,x2,'g'); 
plot(t3,x3,'b'); 
plot(t4,x4,'black'); 
+0

謝謝你的回答,沒有它的工作原理。但是當ii改變了t1,t2,t3,t4的全部並且用t代替它們時,該圖不再顯示,請您告訴我爲什麼會發生這種情況 – user2121

+0

您不能使用'plot(t,x1)',因爲' t'和'x1'沒有相同的大小('plot'發出錯誤) –

+0

你可以使用'plot(t1,x1); plot(t1,x2);'等我想這會產生所需的結果。 – hbaderts