make waves

原文arrow-up-right

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.

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.

Last updated