简体   繁体   English

在MATLAB中创建正弦波

[英]Creating a sinusoidal wave in matlab

I want to create a sinusoidal wave that has the following properties : 我想创建一个具有以下特性的正弦波:

  • a sine wave with f=400Hz amp=1 from 0 to 2s 从0到2s的f = 400Hz amp = 1的正弦波

  • a sine wave with f=200Hz amp=1 from 2 to 3s f = 200Hz amp = 1的正弦波从2到3s

  • a sine wave with f=800Hz amp=2 from 3 to 5s f = 800Hz amp = 2的正弦波从3到5s

Here is my matlab Code : 这是我的matlab代码:

t=linspace(0,5,5000);
x=zeros(1,length(t));
n1=0:1999;
n2=2000:2999;
n3=3000:4999;
x(1:2000)=1*sin(2*pi*400*n1);
x(2001:3000)=1*sin(2*pi*200*n2);
x(3001:5000)=2*sin(2*pi*800*n3);
plot(t,x)

and here is the plot that I had, still it looks not logical at all, So I would like to know the error in my code 这是我所拥有的情节,看起来还是不合逻辑,所以我想知道代码中的错误

在此处输入图片说明

In this type of problem, where you're naturally looking at physical quantities, it's very helpful to be consistent with this all the way through your calculations. 在这种类型的问题中,您自然地要查看物理量,因此在整个计算过程中始终与这一点保持一致非常有帮助。

Specifically, you specify Hz (1/seconds), a physical unit, so when you calculate everything else, you need to be consistent with that. 具体来说,您指定Hz(1 /秒),这是一个物理单位,因此当您计算其他所有内容时,您需要与之保持一致。

To do this in your equation, it's most straightforward to put time directly in the sin function, like sin(2*pi*f*t) . 要在方程式中做到这一点,最直接的方法就是将时间直接放在sin函数中,例如sin(2*pi*f*t) But since you want to break the array apart using different n , it probably easiest to do that and then use t=linspace(0,5,50000) and dt = 5.0/50000 or dt = t(2) - t(1) , and sin(2*pi*400*dt*n1) . 但是,由于您想使用不同的n分解数组,因此可能最容易做到这一点,然后使用t=linspace(0,5,50000)dt = 5.0/50000dt = t(2) - t(1)sin(2*pi*400*dt*n1) Read this as dt*n1 converts the integers in n1 to time in seconds. 看这是dt*n1转换的整数n1时间以秒计。

Note the physical units too: 400 in above is actually 400Hz , and the time is in seconds, so the units of 2*pi*400*dt*n1 and 2*pi*f*t are Hz * s = 1 , that is, the units cancel, which is what you need. 还要注意物理单位:上面的400实际上是400Hz ,时间以秒为单位,因此2*pi*400*dt*n12*pi*f*tHz * s = 1 ,即,单位会取消,这是您需要的。

There is a tendency for programmers to want to define away some unit, like say seconds=1 . 程序员倾向于定义一些单位,例如seconds=1 This is possible and technically correct and can save a multiplication or two. 这是可能的,并且在技术上是正确的,并且可以节省一两个乘法。 It almost always leads to errors. 它几乎总是导致错误。

Note also that you should change from t=linspace(0,5,5000) to something like t=linspace(0,5,50000) . 还要注意,您应该从t=linspace(0,5,5000)更改为t=linspace(0,5,50000) The reason should now be clear: you're looking at frequencies from 400-800Hz, or almost 1kHz , or 1 oscillation per millisecond. 现在,原因应该很清楚:您正在查看400-800Hz或几乎1kHz或每毫秒1次振荡的频率。 To see a sine wave, you'll need to get in a few data points per oscillation, and 50000 points in 5 seconds will now give about 10 points per millisecond, which is barely enough to see a reasonable sine wave. 要查看正弦波,您需要在每次振荡中获取几个数据点,并且5秒内的50000点现在将为每毫秒10个点,这几乎不足以看到一个合理的正弦波。 Or, however you want to think of the calculation, somehow you need to be sure you sample at a high enough rate. 或者,但是,您想考虑一下计算,就需要以某种方式确保以足够高的速率采样。

That is, the specific error that your encountering is that by using integers instead of fractions of a second for your time array, you're taking much too large of steps for the sin function. 就是说,您遇到的特定错误是,对于时间数组,使用整数而不是一秒的小数,对于sin函数您将花费太多步骤。 That's always a possible problems with the sin function, but even if you did plot a sin that looked like a sin (say, by using a frequency like 0.003Hz instead of 400Hz) it would still be incorrect because it wouldn't have the proper time axis. 这总是与一个可能出现的问题sin的功能,但即使你没有绘制sin ,看上去像一个sin (比如,通过使用像0.003Hz,而不是400Hz的频率),它仍然是不正确的,因为它不会有适当的时间轴。 So you need to both get the units correct, and make sure that you get enough data per oscillation to see the sine wave (or whatever it is you happen to be looking for). 因此,您既需要正确设置单位,又要确保每次振荡都获得足够的数据以查看正弦波(或碰巧正在寻找的任何东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM