%% Lab 2 Sinusoidal waves
% This lab uses MATLAB to explore sinusoidal waves.

%% Amplitude
% Plot three sinusoidal waves each having a different amplitude
% for time t from -3 seconds to 3 seconds in steps of 0.01 seconds

t = -3:0.01:3;
A1 = 0.5;
A2 = 1;
A3 = 2;
y1 = A1 * sin(2 * pi * t);
y2 = A2 * sin(2 * pi * t);
y3 = A3 * sin(2 * pi * t);
plot(t, y1, 'r', t, y2, 'b', t, y3, 'c');
xlabel('time (s)');
ylabel('y(t) = A sin(t)');
legend('A = 0.5', 'A = 1', 'A = 2');
title('amplitude of sine wave')


%% Frequency
% The frequency is the number of cycles of the wave that occur over 1
% second. The SI unit for frequency (or counts per unit time) is called
% the hertz (Hz).

% Compare a sine wave of frequency 1/3 Hz versus a sine wave with
% frequency 1 Hz. Over 6s there are 2 cycles of the 1/3 Hz wave and
% 6 cycles of the 1 Hz wave.
A = 1;
f1 = 1/3;
f2 = 1;
y1 = A * sin(2 * pi * f1 * t);
y2 = A * sin(2 * pi * f2 * t);
plot(t, y1, 'r', t, y2, 'b');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t)');
legend('f = 0.33 Hz', 'f = 1 Hz');
title('lower frequency sine wave')
axis([-3 3 -1.5 1.5])


% Compare a sine wave of frequency 3 Hz versus a sine wave with
% frequency 1 Hz. Over 6s there are 18 cycles of the 3 Hz wave and
% 6 cycles of the 1 Hz wave.
figure
f3 = 3;
y3 = A * sin(2 * pi * f3 * t);
plot(t, y3, 'c', t, y2, 'b');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t)');
legend('f = 3 Hz', 'f = 1 Hz');
title('higher frequency sine wave')
axis([-3 3 -1.5 1.5])


%% Phase
% The angle phi is called the phase and is measured in radians. The phase
% determines where in the cycle the wave is when t = 0.


% Compare a sine wave with phase -pi/4 versus a sine wave with
% phase 0. The sine wave with phase -pi/4 appears shifted to the right
% relative to the wave with phase 0. 
figure(1)
A = 1;
f = 1;
phi1 = -pi/4;
phi2 = 0;
y1 = A * sin(2 * pi * f * t + phi1);
y2 = A * sin(2 * pi * f * t + phi2);
plot(t, y1, 'r', t, y2, 'b');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t + phi)');
legend('phi = -pi / 4', 'phi = 0');
title('negative phase angle')
axis([-1 1 -1.5 1.5])

% Compare a sine wave with phase +pi/4 versus a sine wave with
% phase 0. The sine wave with phase +pi/4 appears shifted to the left
% relative to the wave with phase 0. 
figure(2)
phi3 = pi/4;
y3 = A * sin(2 * pi * f * t + phi3);
plot(t, y3, 'c', t, y2, 'b');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t + phi)');
legend('phi = +pi / 4', 'phi = 0');
title('positive phase angle')
axis([-1 1 -1.5 1.5])


%% Interference
% Sinusoidal waves are unique in that the sum two sinusoidal waves having
% the same frequency is another sinusoidal wave, even if the amplitude and
% phase of the two waves are different.

% Constructive interference occurs when the two waves are out of phase
% by an integer multiple of 2*pi. The amplitude of the resulting wave
% is the sum A1 + A2 of the amplitudes of the two waves.
figure(1)
f = 1;
A1 = 1;
A2 = 0.9;
phi1 = 0;
phi2 = 2 * pi;
y1 = A1 * sin(2 * pi * f * t + phi1);
y2 = A2 * sin(2 * pi * f * t + phi2);
y3 = y1 + y2;
plot(t, y1, 'r', t, y2, 'b', t, y3, 'c');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t + phi)');
legend('A1 = 1, phi1 = 0', 'A2 = 0.9, phi2 = 2 * pi', 'A = A1 + A2');
title('constructive interference')
axis([-1 1 -2 2])

% Destructive interference occurs when the two waves are out of phase
% by an odd integer multiple of pi. The amplitude of the resulting wave
% is the absolute value of the difference A1 - A2 of the amplitudes of
% the two waves.
figure(2)
phi2 = -pi;
y2 = A2 * sin(2 * pi * f * t + phi2);
y3 = y1 + y2;
plot(t, y1, 'r', t, y2, 'b', t, y3, 'c');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t + phi)');
legend('A1 = 1, phi1 = 0', 'A2 = 0.9, phi2 = -pi', 'A = A1 - A2');
title('destructive interference')
axis([-1 1 -1.5 1.5])

% When the phase difference is not a multiple of pi, the waves sum
% to a new wave with amplitude somewhere between A1 - A2 and A1 + A2.
figure
phi2 = pi / 2;
y2 = A2 * sin(2 * pi * f * t + phi2);
y3 = y1 + y2;
plot(t, y1, 'r', t, y2, 'b', t, y3, 'c');
xlabel('time (s)');
ylabel('y(t) = A sin(2 * pi * f * t + phi)');
legend('A1 = 1, phi1 = 0', 'A2 = 0.9, phi2 = pi / 2', 'A1 - A2 < A < A1 + A2');
title('interference')
axis([-1 1 -1.5 1.5])


%% Fourier series
% Any periodic wave can be constructed by summing an infinite number of
% sine waves. The sum is called the Fourier series.

% Obviously, we cannot compute the sum of an infinite number of waves.
% In practice, we truncate the sum to a finite number of terms. Here,
% we show the approximation of a square wave using the sum of the
% first 1, 2, 3, and 4 terms of the Fourier series.
t = -1:0.01:1;
L = 0.5;
n1 = 1;
n2 = 3;
n3 = 5;
n4 = 7;

% the first four terms of the Fourier series
y1 = 4 / pi * 1 / n1 * sin(n1 * pi * t / L);
y2 = 4 / pi * 1 / n2 * sin(n2 * pi * t / L);
y3 = 4 / pi * 1 / n3 * sin(n3 * pi * t / L);
y4 = 4 / pi * 1 / n4 * sin(n4 * pi * t / L);

% the approximation created by summing the terms
z1 = y1;
z2 = z1 + y2;
z3 = z2 + y3;
z4 = z3 + y4;
figure(1)
plot(t, z1, 'r', t, z2, 'b', t, z3, 'c', t, z4, 'm');
xlabel('time (s)');
ylabel('y(t)');
legend('first term', 'first two terms', 'first three terms', 'first four terms');
title('square wave approximation using truncated Fourier series')

% the actual square wave
q = ones(1, length(t));
q(51:100) = -1;
q(151:200) = -1;

% the squared difference between the actual square wave and each
% approximation
se1 = (q - z1) .^ 2;
se2 = (q - z2) .^ 2;
se3 = (q - z3) .^ 2;
se4 = (q - z4) .^ 2;
figure(2)
plot(t, se1, 'r', t, se2, 'b', t, se3, 'c', t, se4, 'm');
xlabel('time (s)');
ylabel('squared error');
legend('first term', 'first two terms', 'first three terms', 'first four terms');
title('squared error of truncated Fourier series')

