psyc410_s2x:signal_fft
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
psyc410_s2x:signal_fft [2025/01/19 13:49] – [Complex Waves] admin | psyc410_s2x:signal_fft [2025/01/19 16:16] (current) – removed admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <WRAP centeralign> | ||
- | <typo ff:' | ||
- | Lab 2 - Signals, Frequency, \\ | ||
- | and the FFT | ||
- | </ | ||
- | </ | ||
- | |||
- | \\ | ||
- | \\ | ||
- | |||
- | ====== Information, | ||
- | |||
- | ===== Readings to be completed prior to this lab: ===== | ||
- | * none | ||
- | |||
- | ===== Readings to be completed prior to next week's lab: ===== | ||
- | * Pages 1-20 {{psyc410: | ||
- | * Watch videos #1, #4, #5, and #8 on [[: | ||
- | * This whole series of 10 videos is really great. I have watched them all several times over the years. | ||
- | * Pages 4-17 of {{psyc410: | ||
- | * Read the {{psyc410: | ||
- | * You can skip the following sections: | ||
- | * 4.1, 4.2, 4.3, 4.4 | ||
- | * 5.2, 5.3, 5.4, 5.5 | ||
- | * 6 (skip all) | ||
- | * 7 (skip all) | ||
- | |||
- | <WRAP center round info 90%> | ||
- | Based on feedback from students in prior semesters, I suggest you watch/read the videos/ | ||
- | </ | ||
- | |||
- | |||
- | <WRAP center round tip 90%> | ||
- | You might feel frustrated and find these readings and videos to be inscrutable at first. That's to be expected. I have deliberately chosen these very short readings (and videos) with the intention that they be read, re-read, and re-re-read. You will read them again after next week's lecture and things will start to come into focus. However, that does not mean you should simply skim them. In order to start to make sense this stuff you need to make a good faith effort to understand it each time you're exposed to it! | ||
- | </ | ||
- | |||
- | ===== Goals for this lab: ==== | ||
- | * Get a big-picture understanding of the spectral characteristics of time-series and how the Fourier analysis reveals those characteristics. | ||
- | ===== Software introduced in this lab ===== | ||
- | * none | ||
- | |||
- | ===== Housekeeping ===== | ||
- | * none | ||
- | |||
- | |||
- | ===== Running Code ===== | ||
- | |||
- | <WRAP center round alert 100%> | ||
- | Today' | ||
- | |||
- | **<fc # | ||
- | </ | ||
- | |||
- | ====== Part 1: Signals and Sine Waves ====== | ||
- | |||
- | {{psyc410: | ||
- | |||
- | To understand acquisition and analysis of EEG and fMRI we need to have, at minimum, a basic understanding of sine waves. This is not a trigonometry course so we will not concern ourselves with the math, per se, but it is <typo fc:blue; fw: | ||
- | |||
- | There are plenty of Intro to Trig tutorials online, but I've grabbed a couple from Khan Academy that you might find useful (each are about ~10 minutes long). <wrap em> | ||
- | |||
- | {{youtube> | ||
- | {{ youtube> | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | |||
- | |||
- | A sine wave is a curve with repetitive oscillation of constant amplitude. The sine wave has three properties: | ||
- | * < | ||
- | * <fc:red; fw: | ||
- | * < | ||
- | |||
- | To better understand each of these properties, see the figures and play with the code in the [[psyc410_s2x: | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | The **frequency** of the sine wave is directly related to the **period** and **wavelength**. | ||
- | * wavelength (<m 12> | ||
- | * period (<m 12> | ||
- | * frequency is the reciprocal of the period <m 10>f = 1/ | ||
- | * period is the reciprocal of the frequency <m 10>T = 1/ | ||
- | </ | ||
- | |||
- | {{ psyc410: | ||
- | |||
- | |||
- | |||
- | These three properties (frequency, amplitude, phase) describe the sine wave in the following formula. | ||
- | |||
- | <WRAP center round box 80%> | ||
- | <m 14>A sin(2 pi ft + theta)</ | ||
- | <m 14> | ||
- | <m 14> | ||
- | <m 14> | ||
- | <m 14> | ||
- | <m 14> | ||
- | </ | ||
- | ===== Creating A Sine Wave ===== | ||
- | |||
- | **1.** Run the following code to create your first sine wave. | ||
- | <WRAP center round info 60%> | ||
- | Technically, | ||
- | </ | ||
- | |||
- | |||
- | <WRAP center round info 100%> | ||
- | To run the MATLAB code: | ||
- | |||
- | Option #1 | ||
- | - Download the script by clicking on the tab above the code (Note that MATLAB scripts end with a '' | ||
- | - Open the script in MATLAB | ||
- | - If this is your very first time opening one of the '' | ||
- | - ' | ||
- | - Expand the '' | ||
- | - Press '' | ||
- | - Confirm | ||
- | - That's it. You should not ever have to repeat these steps. From now on these files should open in MATLAB by default. | ||
- | - Press the '' | ||
- | - If you get a warning that the file is '' | ||
- | |||
- | {{ psyc410: | ||
- | |||
- | Option #2 | ||
- | - Open MATLAB | ||
- | - Select '' | ||
- | - Copy and paste the code from this wiki into the untitled document in MATLAB | ||
- | - Save the file using the file name displayed in the tab above the code (on the wiki) | ||
- | - Press the '' | ||
- | - If you get a warning that the file is '' | ||
- | |||
- | Option #3 | ||
- | - Run the code by copy and pasting it directly into the command window | ||
- | - I don't recommend this option because you will not be able to go back and modify anything once it's been run. | ||
- | </ | ||
- | |||
- | <code matlab lab_2_1.m> | ||
- | % MATLAB code | ||
- | % Create and plot a sine wave | ||
- | |||
- | % Define variables | ||
- | srate = 1000; % sampling rate of 1 kHz | ||
- | time = 0: | ||
- | freq = 2; % in Hz | ||
- | amp = 3; % amplitude, or height of the sine wave | ||
- | angle = 0; % phase angle of wave | ||
- | theta = angle * (pi / 180); % phase of the sine wave (radians) converted from the angle parameter | ||
- | |||
- | % Create sine wave | ||
- | sine_wave = amp.*sin(2*pi*freq.*time+theta); | ||
- | |||
- | % The following code will plot your sine wave | ||
- | figure | ||
- | plot(time, | ||
- | % set(gca,' | ||
- | title(' | ||
- | </ | ||
- | |||
- | /*This creates the following figure: | ||
- | {{psyc410_s16: | ||
- | |||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #1 | ||
- | </ | ||
- | </ | ||
- | |||
- | * Save your figure and embed it into your lab report with a label/ | ||
- | * On the figure window select '' | ||
- | * Change format from '' | ||
- | * The default is to save the file into the MATLAB directory. You should save to the desktop instead so that you don't lose track of your files. | ||
- | </ | ||
- | |||
- | <WRAP center round tip 100%> | ||
- | With regard to naming and saving files .... <fc # | ||
- | |||
- | Data analysis (and thus this lab) generally requires creating lots and lots of files, figures, scripts, etc. You will be doing yourself a huge favor to give things descriptive and meaningful titles. By the same token, you should have an organized directory structure in which you put your files. A desktop covered in random files will eventually cause profound sadness. Take the time in advance to think how you want to organize and name things. Future you will be very thankful! | ||
- | |||
- | (As you go you might find that you need to update and refine your system. But better to refine an imperfect system than to be lost in avalanche of your own work) | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | <WRAP center round alert box 80%> | ||
- | **For all lab reports, your figures should be " | ||
- | * images should be high quality (reasonably high resolution) | ||
- | * text should be legible | ||
- | * Images should be cropped, manipulated, | ||
- | |||
- | For tips, see [[psyc410: | ||
- | </ | ||
- | |||
- | |||
- | **2.** Now try running it a few more times. Each time change the '' | ||
- | |||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #2 | ||
- | </ | ||
- | </ | ||
- | |||
- | * Save at least two of your modified sine waves and embed them into your Word document lab report. | ||
- | * Label your figures with the new '' | ||
- | * Describe how these changes affected your sine waves | ||
- | </ | ||
- | ===== Phase Offset ===== | ||
- | |||
- | **3.** To demonstrate how phase angle (< | ||
- | |||
- | < | ||
- | The code requires that the phase be reported in radians (the '' | ||
- | \\ | ||
- | < | ||
- | < | ||
- | |||
- | < | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | So I have written this into the code so that you can enter a phase angle (0-360 degrees) for the '' | ||
- | </ | ||
- | |||
- | <code matlab lab_2_2.m> | ||
- | % MATLAB code | ||
- | % Plot two sine waves offset by 90° | ||
- | |||
- | % Define variables in common for both waves | ||
- | srate = 1000; % sampling rate of 1 kHz | ||
- | time = 0: | ||
- | |||
- | % Define variables for | ||
- | % Sine wave 1 | ||
- | freq = 2; % in Hz | ||
- | amp = 3; % amplitude, or height of the sine wave | ||
- | angle = 0; % phase angle of the wave | ||
- | theta = angle * (pi / 180); % phase of the sine wave (radians) converted from the angle parameter | ||
- | |||
- | sine_wave_1 = amp.*sin(2*pi*freq.*time+theta); | ||
- | |||
- | % Define variables for | ||
- | % Sine wave 2 | ||
- | freq = 2; % in Hz | ||
- | amp = 3; % amplitude, or height of the sine wave | ||
- | angle = 90; % phase angle of the wave | ||
- | theta = angle * (pi / 180); % phase of the sine wave (radians) converted from the angle parameter | ||
- | |||
- | sine_wave_2 = amp.*sin(2*pi*freq.*time+theta); | ||
- | |||
- | % The following code will plot your sine wave | ||
- | figure | ||
- | plot(time, | ||
- | plot(time, | ||
- | % set(gca,' | ||
- | title(' | ||
- | </ | ||
- | |||
- | /*This creates the following figure: | ||
- | {{psyc410_s16: | ||
- | |||
- | **3.** Plot and save the output of the above code. | ||
- | |||
- | **4.** Before moving on to the next section play around with the amplitude ('' | ||
- | |||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #3 | ||
- | </ | ||
- | </ | ||
- | |||
- | * Embed the original and modified figures into your Word document lab report. | ||
- | * Report how your changes to the phase offset affected the relationship between the sine waves | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | ===== Complex Waves ===== | ||
- | The waves we've seen so far are " | ||
- | |||
- | The math to calculate this new complex wave is extremely complicated. It requires that you <checks notes> add numbers together. Hmm, okay I guess it's not terribly complicated. | ||
- | \\ | ||
- | \\ | ||
- | **5.** Let's re-create the waves we created in the last section, but this time we'll also create a third wave that represents the linear combination of the two (i.e., the sum of adding them together). This complex wave will be plotted in green. | ||
- | |||
- | <code matlab lab_1_3.m> | ||
- | |||
- | % MATLAB code | ||
- | % Combine two sine waves | ||
- | |||
- | % Define general variables | ||
- | srate = 1000; % sampling rate of 1 kHz | ||
- | time = 0: | ||
- | |||
- | % Define variables for | ||
- | % Sine wave 1 | ||
- | freq = 2; % in Hz | ||
- | amp = 3; % amplitude, or height of the sine wave | ||
- | angle = 0; % phase angle of wave | ||
- | theta = angle * (pi / 180); % phase of the sine wave (radians) converted from the angle parameter | ||
- | |||
- | sine_wave_1 = amp.*sin(2*pi*freq.*time+theta); | ||
- | |||
- | % Create name for figure legend | ||
- | name1 = sprintf(' | ||
- | |||
- | % Define variables for | ||
- | % Sine wave 2 | ||
- | freq = 2; % in Hz | ||
- | amp = 3; % amplitude, or height of the sine wave | ||
- | angle = 90; % phase angle of wave | ||
- | theta = angle * (pi / 180); % phase of the sine wave (radians) converted from the angle parameter | ||
- | sine_wave_2 = amp.*sin(2*pi*freq.*time+theta); | ||
- | |||
- | % Create name for figure legend | ||
- | name2= sprintf(' | ||
- | |||
- | % Create Sine wave 3 by simply adding sine_wave_1 and sine_wave_2 | ||
- | sine_wave_3 = sine_wave_1 + sine_wave_2; | ||
- | |||
- | % Create name for figure legend | ||
- | name3 = ' | ||
- | |||
- | % The following code will plot your sine wave | ||
- | figure | ||
- | plot(time, | ||
- | plot(time, | ||
- | plot(time, | ||
- | legend(name1, | ||
- | % set(gca,' | ||
- | title(' | ||
- | |||
- | </ | ||
- | |||
- | This creates the following figure (without the annotations): | ||
- | |||
- | {{: | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | Note that at any given location along the x-axis, the amplitude of the new wave is equal to the sum of the values found on our original waves at that same location. In the figure above I report the values of the red, blue, and green lines where they intersect .6 on the x-axis. The value on the green line (the combined wave) is the sum of the other two. | ||
- | \\ | ||
- | \\ | ||
- | This is an important point to consider in the next section. | ||
- | </ | ||
- | |||
- | |||
- | ===== Constructive and Destructive Interference ===== | ||
- | |||
- | **6.** Given what we've learned, what do you think would happen if we combined two identical waves (same frequency, amplitude, and phase)? | ||
- | |||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #4 | ||
- | </ | ||
- | </ | ||
- | |||
- | * Save and embed the new figure you've created into your Word document lab report. | ||
- | * Report what happens when you combine two sine waves with the same frequency, amplitude, and phase. | ||
- | </ | ||
- | |||
- | / | ||
- | |||
- | The figure you create should have a new (green) wave with the same frequency and phase as the others, but double the amplitude. This is called **constructive interference**; | ||
- | |||
- | **7.**Let' | ||
- | |||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #5 | ||
- | </ | ||
- | </ | ||
- | |||
- | * Save and embed the new figure you've created into your Word document lab report. | ||
- | * Report what happens when you combine two sine waves with the same the same frequency and amplitude, but that are 180° out of phase. | ||
- | </ | ||
- | |||
- | In this case we can see that our two waves cause **destructive interference** and the superposition is smaller than either of the individual waves. Actually, in this case because the waves are perfectly out of phase with one another (" | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | Congratulations! | ||
- | </ | ||
- | |||
- | The following video further demonstrates the effects of constructive/ | ||
- | |||
- | {{ youtube> | ||
- | \\ | ||
- | \\ | ||
- | So far we've just been combining two different sine waves, but everything we've learned applies just the same regardless of how many waves we're talking about. For instance, I could combine the following five sine waves (frequencies and amplitudes are summarized in the table) | ||
- | |||
- | ^ Wave ^ Frequency ^ Amplitude ^ | ||
- | | One | | ||
- | | Two | | ||
- | | Three | | ||
- | | Four | | ||
- | | Five | | ||
- | |||
- | {{psyc410: | ||
- | |||
- | and I'd get this complex wave. | ||
- | |||
- | {{psyc410: | ||
- | |||
- | ===== Fourier Transform ===== | ||
- | |||
- | What if we wanted to take //any// given signal and know what simple waves needed to be summed together in order to create it? We would use the [[http:// | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | This is sort of like having a great meal at a restaurant, then going home and trying to figure out the recipe. What were the ingredients that the chef used and in what quantities? If you can figure that out and add them back up again, you've got your great meal! | ||
- | </ | ||
- | |||
- | We will **not** concern ourselves with the math of the Fourier transform, but let's see what kind of results it gives us. To do so, we really just need to look at the signals shown at the end of the last section. Let's say we //started// with this complex signal and wanted to know what basis functions it was made from. | ||
- | |||
- | {{psyc410: | ||
- | |||
- | ===== time-domain ===== | ||
- | Using the Fourier transform to decompose this signal we learn that it is made up of the following simple sine waves, each with their own frequency, amplitude, and phase. We would say that this description is in the **time-domain** because the complex wave is represented by a series of sine waves that each vary over time. | ||
- | |||
- | {{psyc410: | ||
- | |||
- | ===== frequency-domain ===== | ||
- | We can also represent this exact same information in the **frequency-domain**. That is, we can describe the signal in terms of how much each frequency contributes to the total, which is what's done in the plot below. The x-axis now represents **frequency** rather than time and the y-axis still represents amplitude. Compare the bar graph to the table at the end of the last section. You'll see that the Fourier analysis has accurately identified which frequencies are present and at what amplitudes. | ||
- | |||
- | {{psyc410: | ||
- | |||
- | <WRAP center round tip 90%> | ||
- | Those of you paying very close attention will have noticed that our frequency-domain representation (the bar graph) is not displaying //all// of the information in our sine waves. It shows us the **frequency** and the **amplitude** but has ignored the **phase**. That's okay here. The importance of phase and how we represent it are stories fro another time. | ||
- | |||
- | </ | ||
- | |||
- | |||
- | ===== time- and frequency- domain equivalence ===== | ||
- | In summary, these two plots represent the exact same signal. The one on the left represents the signal in the time domain, whereas the one on the right represents the signal in the frequency-domain. But importantly, | ||
- | |||
- | {{psyc410: | ||
- | {{ psyc410: | ||
- | |||
- | <WRAP center round important 100%> | ||
- | |||
- | <WRAP centeralign> | ||
- | <typo fs:x-large; fc:purple; fw:bold; text-shadow: | ||
- | LAB REPORT Part 1 - #6 | ||
- | </ | ||
- | </ | ||
- | |||
- | * The figure below is the combination of three sine waves with the following frequencies and amplitudes: | ||
- | * Wave 1: frequency = 5 Hz, amplitude = 14 | ||
- | * Wave 2: frequency = 7 Hz, amplitude = 10 | ||
- | * Wave 3: frequency = 2 Hz, amplitude = 6 | ||
- | * Use Excel (or any program you'd like) to create a bar graph showing the predicted results of a Fourier analysis. | ||
- | * You can even hand draw this if it's easier. But in that case you'll need to scan you're drawing and put it in your Word document. | ||
- | </ | ||
- | |||
- | {{psyc410: | ||
- | |||
- | <WRAP center round tip 90%> | ||
- | [[psyc410: | ||
- | </ | ||
psyc410_s2x/signal_fft.1737312574.txt.gz · Last modified: 2025/01/19 13:49 by admin