Overview
Lecture 11 – Interpolation, Curve Fitting, and Extrapolation
Step 1 - In your MATLAB Workspace, load Tesla stock price data using this command
load TSLA_stock_history_Mar_2022_2023.mat
NOTE 1: Variable “Stock_Date” is the number days since 7 March 2022
NOTE 2: Date numbers don’t increment uniformly - market isn’t open every day
NOTE 3: Variable “Stock_Price” is the rounded closing stock price
Step 2 – In the MATLAB “Curve Fitter” app, use the “Select Data” button and load in “Stock_Date” as the “X value” and “Stock_Price” as the “Y value”
Step 3 - Make “FIT TYPE” (see figure immediately below) “Interpolant”
Step 4 – “Export” by selecting “Generate Code” (as show in figure above on right side) to create a function called “createFit” that you paste into your script and call using this line:
[fitresult, gof] = createFit(Stock_Dates, Stock_Price)
a. Interpolation: Compare and show “fitresult” and “Stock_Price” values for days 1, 100, and 361 are identical – goal is to show interpolation result passes through the values being interpolated. Show that you can compute a price for day numbers 275.5 and 351.1 – the goal is to show you can now compute values at any date. Hint – use command “fitresult([1 100 361])” to get the fitresult values where the numerical values are the days, not indices
[deliverables: table of “fitresult” values, “Stock_Price” values, and 2 sentences explaining the different prices]
b. Curve Fit - Use “polyfit” command to generate polynomial fits to the data for N = 3, 5, & 7 (type “help polyfit” in the command window for information for what N means) for the same 5 dates provided in part a. Here is an example line of code where N=7:
p7 = polyfit(Stock_Dates, Stock_Price, 7)
price_p7 = polyval(p7,[1 100 361])
[deliverables: table of “Stock_Price” values versus the 3 polynomial fits (as shown below) and 2 sentences explaining the different prices].
c. Extrapolate – compare part a. interpolation vs part b. polyfit solutions for these dates: 380, 400, & 410.
[deliverables: plot all Stock_Price, interpolation, and 3 polynomial curve fits (days 1 through 361) plus the 12 total extrapolation prices as markers, ensure plot has a legend]
NOTE: your actual extrapolation values are not graded, you are only graded on your ability to extrapolate nor will these values be taken seriously to buy or sell stock ?
2. Lecture 12 – Singular Value Decomposition (SVD)
GOAL: Your client wants you to create an algorithm to compress image data to reduce mission data transmission time for a new satellite design. They want you to only keep 90% of the cumulative sum of singular values – meaning keep largest singular values starting with the largest and only keep those that add up to 90% of the total (see plot below).
Use this MATLAB stock image to test your approach and these commands to read image:
X = imread('ngc6543a.jpg');
X = double(rgb2gray(X));
If you want more info on the image, see: Cat's Eye Nebula - Wikipedia
a. Calculate the SVD of the image using the ‘econ’ option.
Plot the singular values (similar to how we did in the
“Lecture_12_SVD_face_demo.mlx” code.
[deliverables: plot of singular values]
b. Calculate the cumulative sum of the singular values
(your plot should look similar to example plot on right which came from
the clown image we discussed in class).
Determine the index (# of singular values) of the cumulative sum when
the percentage of cumulative sum of singular values is 90%.
[deliverables: plot of percentage of cumulative sum of
singular values & report the index]
c. Using the “whos” command, calculate the compression
ratio of original image to compressed image sizes in bytes (original divided by compressed)
[deliverables: show calculations & report your final compression ratio (bytes:bytes) should be greater than 3]
3. Lecture 13 – Fast Fourier Transform (FFT)
Load “chime2.mat” (it’s on CANVAS)
“N” is the number of samples
“audio_recording_data” is the not so good audio recording of the chime
“dt” is the time between samples
“t” is the time array
For example, you can plot or hear the recording:
plot(t,audio_recording_data)
sound(audio_recording_data)
a.
Compute the FFT of “audio_recording_data” using the “fft” command, compute the PSD, and plot the Power Spectral Density (PSD) (like how it was done in class)
[deliverables: plot with labels and legend. X axis must in units of Hz. Like how it was done in class]
b.
Report the first 6 frequencies in Hz that have a PSD value above 1E6
[deliverables: Table of frequencies in Hz]
c.
Denoise the data by the following (see class notes and code):
(1) Magnitude filter - only keeping PSD values above 1E6
(2) Frequency filter – only keep PSD values below 2000 Hz
[deliverables: plot PSD of denoised data – plot should look similar to the one below]
d.
Compute inverse FFT using the “ifft” command and plot the time domain signal from 0 to just under 2 seconds
[deliverables: plot time domain denoised data]