Simple programs, scripts, debugging
First we look at ...
Accessing MATLAB
Locally
Via Citrix
Via Terminal/Command Line
Getting used to the different windows in MATLAB
Simple commands and the command window
Scripts vs. functions vs. function files
Then we demonstrate/practice some examples. Recall that the Fibonacci recurrences is given as:
$$\begin{align} F_0 &= 0\\ F_1 &= 1 \\ F_n &= F_{n-1} + F_{n-2} \end{align}$$
Can we write a function that returns the n-th term in this sequence?
Simple Fibonacci
fib_simple (generic function with 1 method)
Implement the above function in MATLAB. Then click on the arrow in the below cell to expand it. Compare.
Simple Fibonacci - MATLAB
function fib = my_fib(n)
% Generates the n-th term in the Fibonacci sequence
if n < 2
fib = n ;
else
fib = my_fib(n-1) + my_fib(n-2);
end
end
There is a problem with the above function ... it is slow.
In the version above, the function calls itself twice to calculate the two previous terms, which are then added to find the current term. This process continues until the n-th term is calculated. However, this version has exponential time complexity and can be quite slow for larger values of n, especially since it recalculates the same terms multiple times.
Better Fibonacci
A better approach would be an iterative one.
fibonacci (generic function with 1 method)
Can you implement the above in MATLAB?
Note: MATLAB doesn't do swap assignments like above, so will need to make use of a temporary variable
Better Fibonacci - MATLAB
function fib = my_fib(n)
% Generates the n-th term in the Fibonacci sequence
if n < 2
fib = n;
else
n_minus1 = 1;
n_minus2 = 0;
for i = 2:n
fib = n_minus1 + n_minus2;
n_minus2 = n_minus1;
n_minus1 = fib;
end
end
end
The above function uses a loop to iteratively calculate each term in the sequence. The first two terms, 0 and 1, are handled as special cases. For each subsequent term, the function adds the two previous terms to find the current term.
Advanced topic
If you still want to implement recursive Fibonacci, you can make use of memoization. this is the process of storing already calculated values in memory so we can reuse them instead of performing the computation again.
Memoized Fibonacci
Memoized Fibonacci - MATLAB
function fib = fibonacci(n, memo)
% Generates the n-th term in the Fibonacci sequence using memoization
if n <= 0
fib = 0;
elseif n == 1
fib = 1;
elseif n <= length(memo) && memo(n) ~= 0
fib = memo(n);
else
memo(n) = fibonacci(n-1, memo) + fibonacci(n-2, memo);
fib = memo(n);
end
In this version, an additional argument memo is added to the function. The memo argument is a vector that stores the results of previous calculations. Before calculating a term, the function checks if the result is already stored in memo. If so, it returns the stored result. If not, it calculates the result and stores it in memo for future use.
To use this function, you would call fibonacci(n, zeros(1, n))
, where n
is the desired term and zeros(1, n)
is the initial memo vector of zeros. The size of memo is set to n
because the maximum term that needs to be calculated is n
.
Plots & FFT
In this section we transition to talking about the fft
function. For this we will use audio files as our signals.
Load up an audio file and plot its waveform. Download the audios file from Ed. They are called sound_t.wav
and sound_b.wav
.
Here
y
- Waveformfs
- Sampling frequencynbits
andopts
- Encoding options.
As you can see from examining the fs=
16000 Hz
Take the Fourier Transform and plot the magnitude and phase components
In the above fftfreq
is a function in Julia that will generate the frequency vector for us. MATLAB doesn't have an equivalent so you must generate the frequency vector yourself and in addition use the fftshift
function!!
As we can see the phase plot isn't all that informative. But what is this negative frequency?
Check: Lecture note 8 for details.
We are interested in the portion 0 to 8 kHz because we only have reliable information at up to half the sampling frequency (here 16 kHz). So let us redo the plot.
MATLAB Version
% READ the file sound_t.wave after downloading it from EdStem.
% Below code intentionally leaves out generating the frequency vector.
% Exercise!!
[y, Fs] = audioread("sound_b.wav");
FY = fft(y);
figure
subplot(2, 2, [1 2])
plot(y)
title("Waveform")
subplot(2, 2, 3)
plot(fftshift(abs(FY)))
title("Magnitude spectrum")
subplot(2, 2, 4)
plot(fftshift(angle(FY)))
title("Phase spectrum")
Audio engineering?
In the next bit we are going to zero out the complex coefficients corresponding to a few frequencies and then reconstruct the wave form use the Inverse Fourier Transform.
Specifically we are zero-ing out the coefficients above 800 Hz.
Now we reconstruct the signal
Exercise: can you do the same in MATLAB?
Folding code