2017-06-06 205 views
0

我試圖產生一個正弦波,它在0.004秒的時間間隔內有規律地斬波(輸出將是時間0到0.004秒處的正弦波,然後在0.004秒到0.008秒之間輸出爲零second.This將繼續以類似的方式)。正弦波繪圖

我試過下面的代碼,但它不會生成正確的輸出。

f=10000000000; 
k=0; 
for i=0:0.004:1 
    k=k+1; 
    if(mod(k,2)~=0) 
     t=i:0.001:i+0.004; 
     y=sin(2*3.14*f*t); 
     plot(t,y); 
    else 
     t=i:0.001:i+0.004; 
     y=0; 
     plot(t,y); 
    end 
end 
+0

關於你想要的輸出的更多描述可能會有幫助:什麼部分你想砍? –

+0

我編輯了我的描述。 –

回答

0

對於Matlab中的繪圖,您需要x值的數組以及要繪製的整個節的相應y值數組。所以你想先創建一個對應於從0到1.004的時間的x數組(如果我正確理解你的代碼),並創建一個長度爲的y數組,並用相應的正弦和0值填充它。

f = 10000000000; 
% generate evenly spaced x axis of 251*n+1 points evenly spaced between 0 and 1.004 (i.e. spacing between two points is 0.004/n). n should be an integer. 
t = linspace(0, 1.004, 251*n+1); 
% generate unchopped sine 
y = sin(2*3.14*f*t); 
% set desired intervals to zero 
for i=1:251*n+1 % for each index in y 
    % if the corresponding entry in t is a multiple of 0.008, backfill y with zeros for 0.004s. 
    if(mod(t(i), 0.008)==0) 
     if(i>1-n) 
      for j=i-n:i 
       y(j) = 0; 
      end 
     end 
    end 
end 
plot(t, y) 
+0

我試過你的代碼,但它導致error.It說試圖訪問y(3.984),索引必須是一個正整數或邏輯 –

+0

@Ishant Tiwari,我以前的方法不適用於一般n,我現在編輯它應該以正整數n工作。例如,我們試圖訪問對應於0.004和0.008之間部分的y的索引,並且索引像計數一樣工作,所以這些必須是正整數。 –

+0

仍然沒有工作。 –