make waves

原文

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!
%-------------------------------------------------------------------------------------

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');
%-----------------------------------------------------------------------

Last updated