# make waves

[原文](http://bishoptechbits.blogspot.ie/2010/07/simplified-introduction-to-time.html)

## Creating a simple sine wave

To create a sine wave, you need four parameters: D = duration of the signal, here specified in seconds S = sampling rate, samples per second F = frequency, in Hz P = phase, as an angle

```
%-------------------------------------------------------------------------------------
% make_sig.m
%-------------------------------------------------------------------------------------
D = 1; % signal duration, 1 second
S = 1000; % sampling rate, i.e. N points pt sec used to represent sine wave
F = 50; % frequency in Hz
P = 1 % NB. 2 pi radians = 360 degrees
T = 1/S; % sampling period, i.e. for this e.g. points at 1 ms intervals
t = [T:T:D]; % time vector %NB corrected from previous version
myphi=2*pi*P;
mysig = sin(2*F*t*pi+ myphi); % sinusoidal signal; 2*F*t*pi is freq in radians
figure; plot(t,mysig);
ylim([-2,2])
xlim([0,2])
title('a simple sine wave')
xlabel('Time (seconds)');
ylabel('Amplitude');
% sound(mysig); %You can play the sound if you like!
%-------------------------------------------------------------------------------------
```

If you don't understand about sampling rate, frequency or phase, experiment with altering values of S, F and P to see the effect on the wave. Hint: it is easiest to see how changing phase alters the signal if you select a frequency less than 10. Note that if you select a value of S that is less than twice F, the waveform changes, **because the sampling is too slow to capture the fluctuations**. 2\*F is known as the Nyquist frequency, which is the lowest sampling frequency that can be used to correctly identify a sine wave of frequency F. ![](https://1237447543-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4ZaszQkuLof2XL0b_v%2F-M4Zav0QOp8dmT2IkJTc%2F-M4Zb-2TOdExZni-atgG%2F1.png?generation=1586530891506166\&alt=media)

## Creating a complex wave

Add some sine waves together, with different frequencies and phases. The next program is based on make\_sin, but adds together waves of different frequency and phase.

```
%-----------------------------------------------------------------------
% create_complex.m
%-----------------------------------------------------------------------
D = 1.2; %signal duration
S = 1000; % sampling rate, i.e. N points pt sec used to represent sine wave
F = [30 40 100 200]; % 4 frequencies in Hz
w = 2*pi*F; % convert frequencies to radians
P = [0 .5 .25 .3]; % 4 corresponding phases
A = [1 .5 .3 .2]; % corresponding amplitudes
T = 1/S; % sampling period, i.e. for this e.g. points at 1 ms intervals
t = [T:T:D]; % time vector %NB this has been corrected from previous version
mysig=zeros(1,length(t)); %initialise mysig
myphi=2*pi*P; % compute phase angle
nfreqs=length(F); % N frequencies in the complex
% Add all sine waves together to give composite
for thisfreq=1:nfreqs
mysig = mysig+A(thisfreq)*(sin(w(thisfreq)*t + myphi(thisfreq)));
end
figure; plot(t,mysig);
ylim([-2,2])
xlim([0,2])
title('a complex wave')
xlabel('Time (seconds)');
ylabel('Amplitude');
%-----------------------------------------------------------------------
```

![](https://1237447543-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4ZaszQkuLof2XL0b_v%2F-M4Zav0QOp8dmT2IkJTc%2F-M4Zb-2Vrv963zjbZjNI%2F2.png?generation=1586530892146808\&alt=media)
