2016-03-28 75 views
0

我试图乘以正弦波/余弦波递归,但我不知道为什么我的答案如此不同。 Y2蓝色就是我试图让,但FOR循环是Ÿ红色是什么产生的任何想法如何解决FOR循环ÿ?看下面的情节和代码?在一个循环中递增乘以正弦波/余弦波在matlab /倍频程中

Fs = 8000;% Sampling frequency 
t=linspace(0,1,Fs); 

y=zeros(1,length(t)); 
y = .5*sin(2*pi*2*t); 
for ii=1:1:3  
    y=y.*y; 
end 
plot(y,'r') 
hold on 

y2=(.5*sin(2*pi*2*t)).* (.5*sin(2*pi*2*t)).*(.5*sin(2*pi*2*t)); %should look like this 
plot(y2,'b') %both plots should look like this 

Plot PS:我使用的是八度3.8.1这就好比MATLAB

+0

你'for'循环是好的,但你的想法是错误的。 ;) –

回答

3

你在这里乘以八倍

y = .5*sin(2*pi*2*t); 
for ii=1:1:3  
    y=y.*y; 

ii=1:1:3是包容性的,所以你做y=y.*y三次。

第一次变得Y = Y^2,

第二时间之成为Y 1 4 = Y^2 * Y * 2

它变成:Y^8 = Y^4 * Y第三时间^ 4


这将是一个解决方案:

Fs = 8000;% Sampling frequency 
t=linspace(0,1,Fs); 

y=zeros(1,length(t)); 
y = .5*sin(2*pi*2*t); 
result = ones(1,length(t)); 
for ii=1:1:3 
    result=result.*y; 
end 
plot(result,’r’) 
hold on 

y2=(.5*sin(2*pi*2*t)).* (.5*sin(2*pi*2*t)).*(.5*sin(2*pi*2*t)); %should look like this 
plot(y2,'b') %both plots should look like this 
2

在所有的迭代,Y * Y作更新Y的平方。

Fs = 8000;% Sampling frequency 
t=linspace(0,1,Fs); 

y=ones(1,length(t)); 
x = .5*sin(2*pi*2*t); 
for ii=1:1:3  
    y=y.*x; 
end 
plot(y,'r') 
hold on 

y2=(.5*sin(2*pi*2*t)).* (.5*sin(2*pi*2*t)).*(.5*sin(2*pi*2*t)); %should look like this 
plot(y2,'b') %both plots should look like this 

也许您想要这样的代码。

+2

至少在Matlab中,你甚至可以简化为'y = y。^ 3;'。他做错的一个解释也是很好的。 ;) –