1
votes

I want to create a sinusoidal wave that has the following properties :

  • a sine wave with f=400Hz amp=1 from 0 to 2s

  • a sine wave with f=200Hz amp=1 from 2 to 3s

  • a sine wave with f=800Hz amp=2 from 3 to 5s

Here is my matlab Code :

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

enter image description here

1
Read about aliasing and Nyquist frequency. There are other mistakes in your code (you mean t(n1+1) not n1, you want time in the equation, not indices), but you are simulating 5 seconds. That is 4000 periods of 800Hz. You can not sample that signal with 5000 samples (linspace). Read what Nyquist frequency is and what it says about sampling singnals - Ander Biguri
So if the Fs=40Hz it should be like that the code for the 1st wave ? : t=0:1/fs:2000-1/fs x=1*sin(2*pi*t*400 ) thank you - Khaled Arja

1 Answers

0
votes

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.

To do this in your equation, it's most straightforward to put time directly in the sin function, like 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). Read this as dt*n1 converts the integers in n1 to time in seconds.

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.

There is a tendency for programmers to want to define away some unit, like say 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). The reason should now be clear: you're looking at frequencies from 400-800Hz, or almost 1kHz, or 1 oscillation per millisecond. 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. 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. 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. 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).