% ECE 1250 Lecture 5 %------------------------------------------------------------------------------- % Advanced Indexing, finish remaining topics. (See handout on website.) %------------------------------------------------------------------------------- %------------------------------------------------------------------------------- % Writing script files. %------------------------------------------------------------------------------- % Change directory to where script files are located so Matlab¨ can find them, % or use the Set Path command under the File menu to add the folder with % the script files to where Matlab¨ looks for commands. % Use .m extension on file name. Put in commands just as you would at the % >> prompt in the command window. Here's a simple example of a script file % we shall call my_magic.m % my_magic.m Script to print a 3 x 3 magic square. A = magic(3) display('That''s a magic square!') return % To execute the script file my_magic.m, type the filename without the .m >> my_magic A = 8 1 6 3 5 7 4 9 2 That's a magic square! % Be careful not to create files with the same name as a Matlab¨ command. % For example, say you create a file called plot.m. When you use the plot % command, Matlab¨ will execute the first plot.m file it finds in the % search path. If your plot.m was found first, you would no longer have % access to Matlab¨'s plot command. % How to get rid of your plot.m command once it gets into Matlab¨ (and gets % compiled and stored in memory so the file is no longer accessed directly)? % 1) Remove or rename your file. % 2) Use the clear command to clear the filename, e.g., >> clear plot % The next time you type "plot", Matlab¨ will go through the search path % again and find the correct file. %------------------------------------------------------------------------------- % Fourier Analysis (finding what sinusoids are in a signal) %------------------------------------------------------------------------------- % The Fast Fourier Transform, or FFT, computes what sinusoids are in a waveform. >> y_spectrum = fft(y); % No matter how many points are in y, the fft analyzes frequencies from 0 to the % sampling rate of y. For sounds we have used, the sampling rate is 8000/sec. % y_spectrum has as many points as y, but those points represent frequencies % from 0 to 8000 Hz. % The fft command produces complex numbers that encode the amount of cosine and % sine at each frequency. We use abs() to find the magnitude of the cosine and % sine combined at each frequency. >> mag_y_spectrum = abs(y_spectrum); % The plot of the fft shows us what frequencies are in a signal. >> plot(abs(fft(y))) % We can go the other direction, too, to create a signal from its frequencies. >> y = ifft(y_spectrum); % The "i" in ifft stands for "inverse". % We can alter a sound by changing its spectrum. A filter reduces certain % frequencies, for example. We can make digital filters to alter a sound, % as we will see in the lab later on. %------------------------------------------------------------------------------- % Example script file creating sound effect: see my_script.m % Example script file to analyze students' test scores: see my_script2.m %-------------------------------------------------------------------------------