angle
in Matlab/Mathscript.
The angle
also works for a vector of complex values,
e.g. samples of a frequency response taken at different frequencies.
The angle
command returns a phase between [-pi/2, pi/2],
which is known as wrapped phase.
One could compute the unwrapped phase by applying the
phase
command to a vector of complex values.
Here is a simple example illustrating unwrapped phase. Consider an ideal delay. The transfer function is H[z] = z-1. The frequency response is H[ej w] = e-j w. The phase is -w. If we were to plot the phase response as arctan( e-j w ), then we would see jumps of discontunity at ..., -3 pi/2, -pi/2, pi/2, 3 pi/2, ... instead of a straight line.
load gong n = 1 : Fs; f0 = 440; w0 = 2*pi*f0/Fs; noteSound = cos(w0*n); sound(noteSound);The command
load gong
loads the
samples in the gong sound to the variable y
and sets the sampling rate Fs for the sound card.load gong
with Fs = 8192;
or load a different sound file of a similar format.
The following Matlab code assumes that the
variable noteSound
holds the samples
of the sound and plots the magnitude of the
frequency content in y
frequencies
-Fs/2 to Fs/2.
The amplitude A is shown on a deciBel (dB) scale,
i.e. 20 log10 A.
y = noteSound; freqPoints = (-1 + 2*cumsum(ones(1,length(y)))/length(y))*Fs/2; magValuesIndB = 20*log10(fftshift(abs(fft(y)))); plot( freqPoints, magValuesIndB );
%%% Set the sampling rate, Fs, of sound card load gong wholeNotesWesternScale = [ 440 494 523 587 659 698 784 880 ]; n = 1 : Fs; %%% Play one note at a time for f0 = wholeNotesWesternScale w0 = 2*pi*f0/Fs; noteSound = cos(w0*n); sound(noteSound); endNote: For LabVIEW Mathscript users, you can replace
load gong
with Fs = 8192;
or load a different sound file of a similar format.
Download homework7sound.mat.
Here is Matlab code to load the homework7sound file, play it and plot its frequency content:
load homework7sound.mat sound(homework7sound); y = homework7sound; freqPoints = (-1 + 2*cumsum(ones(1,length(y)))/length(y))*Fs/2; magValuesIndB = 20*log10(fftshift(abs(fft(y)))); plot( freqPoints, magValuesIndB );
The function x(t) is odd symmetric about the origin. Hence, the terms a0 and an will be zero.
Here is example Matlab code that could be used to generate
x(t) and the Fourier expansion for n=1, x1(t),
for two periods.
The Matlab expression rectpuls(t)
is a rectangular
pulse centered at the origin with non-zero extent over
-0.5 ≤ t ≤ 0.5.
We need two rectangular pulses, i.e. one for each period
being plotted.
The example code uses the hold
command to
superimpose plots in Matlab on the same figure.
T0 = 2; f0 = 1/T0; w0 = 2*pi*f0; t = -1 : 0.01 : 3; x = sin(2*pi*t) .* ( rectpuls(t) + rectpuls(t - T0) ); b1 = 1; %%% Use your calculated value x1 = b1 * sin(w0*t); hold on; plot(t, x, 'b-'); %%% Plot in blue solid lines plot(t, x1, 'r--'); %%% Plot in red dashed lines hold off;
If you experience a division by zero in computing one of more the bn terms, then you might try to simplify the expression of bn for that value of n using L'Hopital's Rule.
The impulse train is even symmetric about the origin. Hence, the bn terms will be zero.
The Fourier series expansion of a periodic signal will be exact if an infinite number of terms is used.
Here is Matlab code to visualize what the Fourier series expansion is converging to. The code arbitrarily sets the fundamental period to 1 s. Try out N = 10 and then N = 100 and finally N = 1000.
T0 = 1; f0 = 1/T0; w0 = 2*pi*f0; tmin = -0.5*T0; tmax = 0.5*T0; t = tmin : (tmax-tmin)/1000 : tmax; %%% Fourier series expansion N = 10; a0 = 1; fourierSeriesExp = a0*ones(1,length(t)); for n = 1:N an = 2; fourierSeriesExp = fourierSeriesExp + an * cos(n*w0*t); end plot(t, fourierSeriesExp);