Fourier Analysis

advertisement
ENGN/PHYS 225—Winter 2016
Fourier Series and Transforms: Real world applications
Due date: Monday, March 28, 2016, in class
Background
Why do two instruments sound different even when the same note is struck, sung, plucked?
How we can pluck out important data about how a vibrational modes of a structure based on
displacement vs. time data?
How can we distinguish between a brain thinking “move my foot” vs. “move my right hand”
via analysis of electrical recordings?
How can we enhance the quality of a signal corrupted by noise?
Survey says: Fourier series and transforms. Ding, ding, ding!
The purpose of this assignment to highlight real-world applications and implementation of
Fourier analysis, and connect those results back to theoretical results, thus completing the circle of
life.
Materials and Methods
We’ll make use of MATLAB to analyze real-world data. To help you navigate through this assignment, explicit directions and hints have been written in (excruciating?) detail. Please
read them carefully so you can follow along with ease.
Data Sets
Data sets are provided to you in .mat file format (biomedical data) or .wav format (sound files),
which can be fed directly into matlab using the provided matlab function: freqAnalysis.m.
You will need to download the .zip file with all necessary files at: http://home.wlu.edu/
ericksonj/engn225_w2016/probsets/fourier_matlab.zip.
Be sure to unzip (“extract”) the
~
files before proceeding. For instance, in the IQ center, use 7zip, right click and you should have a
menu option to unzip or extract. Keep all of these files in one folder. Note carefully the directory
(folder) where you unzipped these files.
1
MATLAB
You need to tell matlab where these files live, so that they can be accessed. The easiest way to do
this is to copy and paste the full path to the folder, then go to the matlab command window and
type for example:
>> addpath(‘h:\WandL\Courses2015\MathMethods\probsets\fourier’)
Of course, replace the directory in the example above to be the one on your account/hard drive.
Fourier Synthesizer
You will also need to use a digital fourier synthesizer. There is a really nice, full featured one to
download at: http://www.falstad.com/fourier/ Note: depending on your browser settings, this
java applet may run directly. I use google chrome and had to download the files and run them
locally. To do so, click on the link “Download zip archive of this applet.” Save the files to your
hard-drive. Unzip them. Then double click on the file fourier.jar to launch the synthesizer
(despite what the directions on falstad.com say, don’t double click index.html)
Problems/Experiments
1
Total Eclipse of the Heart
This mini-experiment uses actual electrocardiogram (ECG) data. What’s an ECG? You probably
already know the answer, as you’ve very likely seen in the movies and/or melodramatic NBC
dramas (I’m looking at you, “ER”), where a patient has electrodes connected to their torso and
the signal looks like blip....blip....blip. That’s the electrical signal generated by your heart as a
result of the nervous and muscular activity shuttling around a bunch of ions (aka electrical current
flow). The frequency content of the cardiac signal is used in hospital settings to diagnose abnormal
heart conditions and—near and dear to my heart—monitor the overall health of a prematurely
born baby, to name just a couple of applications. Let’s get started!
ANALYZING REAL WORLD DATA
Crack open the data in the matlab data file ecgdata.mat. The easiest way to do this is this is
go to the main matlab window. On the “Home” tab, near upper left, click “Import Data” (green
down-pointing arrow icon). Navigate to the ecgdata.mat file and select it. Matlab should offer to
“Create variables matching preview” with the variables of t and v1. Click “finish”. The variables
should now be loaded in the matlab workspace The former is a list of times (vector or array) at
which the heart signal was measured, the latter is the voltage varying over time. Also loaded into
the workspace is the variable Fs. This is the sampling rate of the data in units of Hz. It should be
F s = 30 Hz = 30 samples/s in this case.
2
Figure 1: Prototypical ECG data, correlated to phases of the heartbeat.
To verify, plot the signal. You can do this with the plot command like this:
>>plot(t, v1)
You should get a picture that looks like the one in Figure 2.
Figure 2: ECG data from human subject.
Assuming yes, time to chug full speed ahead. First of all, how many beats per minute (bpm)
was this person’s heart beating at?
Now it is time to perform a Fourier analysis: what frequencies are present in the ECG signal?
We’ll tackle this question first empirically, then theoretically.
The empirical part is done with matlab and the fft function. You can learn more about this
function by typing:
>> doc fft
The tutorial below is based off of the second example in this documentation.
Pay particular attention to the examples provided.
3
Specifically, you can compute the fourier transform of the signal v1 by doing the following:
>> L = length(v1)
The length() function returns the number of samples in v1.
>> NFFT = 2^nextpow2(L);
This computes the next power of of 2 from the length of v1. For instance, if L = 1000, then NFFT
will be 1024; This is needed as part of the fast fourier transform algorithm, which works by dividing
and conquering the data in half each pass through.
>> Y = fft(v1, NFFT)/L;
This computes the Fourier transform over a a discrete (but quasi-continuous) range of frequencies. Recall that Y acts like the cn ’s, which are complex numbers.
>> f = Fs/2*linspace(0, 1, NFFT/2+1);
This computes the discrete set of frequencies at which the FFT was computed. The factor of F s/2
is very important. It is known as the Nyquist frequency . Theoretically, it is not possible to
discern frequencies in the signal oscillating faster than F s/2, hence that value is the upper range
for f . You can learn more about linspace(.) by typing: >> help linspace
>> Ysub = Y(1: NFFT/2+1);
This grabs the subset of Y (the result returned from FFT), which correspond to frequencies
≤ F s/2.
>> magnY = 2*abs(Ysub);
This computes the magnitude of the Fourier transform coefficients; think of this essentially as
|cn | + |c−n | = 2|cn | that you are plotting.
>> plot(f, magnY);
>> xlabel(‘Frequency (Hz)’); ylabel(‘FFT(v1)’); title(‘FFT of ECG data’)
Plot the results, with axis labels.
If all went according to plan, you should end up with a figure that looks like that shown in
Figure 3. Note that you can use the “datatip” tool (plus sign along arc; top middle of tool bar) to
explore actual values at which the peaks occur.
MODEL AND THEORETICAL RESULT
Let’s come to grips with why the FFT looks the way it does by computing the Fourier Series
coefficients, modeling our heartbeat signal as follows. Let’s say the QRS begins at time t = 0, such
that the peak voltage occurs a short time afteward. The signal is periodic with T = 1.1 s. Then
4
Figure 3: FFT of ECG data. The fundamental frequency is 1.11 Hz (66 bpm). Many harmonics
strongly contribute.
we have 3 time domains to consider:


for t ≤ 0.067 s
7500 t,
f (t) = 502.5 − 7500 (t − 0.067), for ≤ 0.067 < t ≤ 0.134 s


0
otherwise up to t = 1.1 s
The factor of 7500 is the steep slope (units of muV/s) during the QRS complex. Note that this
triangular pulse width of the modeled QRS complex (0.134 s) is small compared to T (1.1 s).
Given this definition for f (t) compute the Fourier series coefficients cn , and then compute the total
contribution of the nth harmonic as |c−n | + |cn | = 2|cn |.
Make a stem plot of your answer. A stem plot is simply a graphical representation, in this
case, of 2|cn | vs. n (integer harmonic number).
Compare and contrast your answer obtained in MATLAB for real world data vs. the theoretical
result you just obtained given the model of the QRS complex above.
SYNTHESIZE ME
How do a bunch of harmonics add up to a pulse-like QRS complex. To get some intuition for the
results obtained fire up the Fourier synthesizer you previously downloaded from falstad.com. Add
in harmonics in the proper proportion, one at a time, noticing how each harmonic contributes to
the final waveform. Why should most (all) harmonics contribute equally in order to build up a thin
“pulse” waveform? 1
1
If the pulse gets infinitely thin and infinitely tall, we’ll end up with a Dirac delta function! These functions
are of great importance, especially useful in quantum and E&M.
5
2
Mind Reader
Figure 4: Electroencephalogram (EEG) scalp cap. Electrodes are distributed around the skull.
Don’t worry, this nice person isn’t getting her brains sucked out by a syringe. Rather, the experimenter is putting some electrolyte loaded gel beneath the electrode and her scalp to make for a
good electrical connection.
The electroencephalogram (EEG) is widely used for studies of brain and behavior. More recently, many labs are pursuing reading out brain activity for neuroprosthetic applications. The
EEG measures the electrical signal generated by brain activity associated with a certain state of
mind. In terms of neuroprosthetics the basic idea goes like this. The patient may have lost control
of limbs, but they can still think about moving an arm or a leg. These are distinct brain states—
”move arm” vs. “move leg”. This is termed a two class motor imagery task. Presumably, these two
states can be differentiated by their differing electrical activity. This could be done in automated
fashion using fancy AI techniques (that are actually must closer to your finger tips than you think,
especially now that you are nearing the end of math methods!). The computer would recognize
“Oh, we measured a signal associated with thinking about moving the arm forward and right, so
let’s make our robotic prosthetic arm do that!”. Then various motors are turned on, etc. You can
see more state-of-the-art prosthetics at: https://www.youtube.com/watch?v=flacabyMGQk. For
now, on with the Fourier analysis!
In the matlab command window, clear off your old results by doing:
>>clear all
Next, import the data stored in the data file eegdata.mat. It after clicking “finish” MATLAB you
may get a dialogue box warning you that variables in the workspace of the same name already exist.
It is safe to overwrite them, so click OK. At this point you should have varables in the workspace:
y1, y2, t, and Fs.
You can plot the data and should see signals that look like the ones in Figure 5:
The EEG data set you are about to work with is a two class motor imagery task from: http:
//bnci-horizon-2020.eu/database/data-sets. While the patient is donning the head cap, she
was asked to think about moving her foot or her arm (but not both). The recording from one
6
Figure 5: EEG trials for motor imagery of moving the hand (top) and foot (bottom). Is the
frequency content of these signals different enough to tell them apart?
electrode from the first trial (“move arm”) is stored in y1. The “move foot” trial EEG recording is
stored in y2. The time vector t specifies the times at which the voltage was sampled. The sampling
rate is stored in Fs (= 512 Hz).
The core question the authors of the study were trying to address is whether the FFT can be
used to discriminate between the two tasks. Can it? Let’s find out!
Compute and plot the FFT of the first and second trial results. Observe that a lot of the action
takes place at lower frequencies, so it will likely be very helpful to limit the range of frequencies
you are viewing. You can do this by typing in the matlab command window:
>>xlim([0 15])
This limits the x-axis to display between 0 and 15 Hz only.
Back to the addressing the main question: Based on the plots of voltage vs. time can you
discern any statistically significant differences between the recordings? How about for the FFTs
for trial 1 and trial 2, can you discern any real difference? What frequencies seem to be produced
more strongly for imaginging moving one’s hand vs. the foot, and vice-versa? Make a quantitative
argument stating whether and how a computer could tell apart the two motor tasks based on the
FFT. There is no single right answer, but build a case for whatever you ultimately claim.
7
3
Building vibrations: What’s all that noise?
You’ve just been hired as a young, hotshot mechanical engineer with USAID. The first assignment
is to analyze the vibrational modes of a 4-story building being constructed in downtown Lex. The
natural frequencies of vibration are crticially important to know. Prior to construction it must be
ascertained that there are no forces (perhaps wind or A/C fans on the top of the building) acting
at any of these freuqencies, else the building could resonate and then very bad things happen. So
you get out some handy strain gagues, attach them to the walls of the building and make some
measurements for displacement over time.
REAL WORLD DATA
Real world data is always corrupted by noise to some extent. Ideally, an experimenter would make
clean recordings with the underlying signal of interest having a much greater magnitude than the
noise. Ideally. There are many cases in the real-world when we are interested in knowing the
underlying frequency of the signal of interest—shooting lasers at high-speed airflow (hello, Prof
Kuehner’s lab), gigahertz optics for waveguides, and mechcanical vibrations of civil structures.
This problem focuses on the mechnical vibrations of a 4 story building, but could be generalized
to any number of real world scenarios. The building is assumed to have equal spring constants and
masses for all floors: k = 45698 N/m, and m = 5000 kg. Yours truly simulated this building, then
added Gaussian (random) noise to obscure the underlying vibrations of each of 4 floors.
Start by clearing off your matlab workspace (>> clear all), then loading the data in vibrationdata.mat.
You should see the variables t, Fs, and x in your workspace. In this case, the variable x is a 4×
542 matrix. Thus, displacement for the first floor is encoded in the first row, the second floor in
the second row, etc. You can parse out this data, like this:
>> x1 = x(1, :);
This is MATLAB shorthand notation for indexing the first row and all columns (that’s what
the colon does). Then you can plot the data, per standard operating procedure:
>>plot(t, x1);
Doing so should produce a figure that looks akin to that in Figure 6:
Figure 6: Simulated vibration data for a 4 story building. Noise has been added to obscure the
underlying vibrations over time.
8
Now the question becomes: If you saw this raw data, could you discern the underlying vibrational frequency(-ies) of the first floor? If yes: Teach me, oh master! In general, humans are really
bad at parsing out underlying frequencies by visual analysis. (Conversely, our ears/auditory are
exceptionally good at it!) Well, how can you tease out frequency content from what looks like a
really messy plot? Enter Fourier transforms. Compute and plot the FFT of the vibrational data.
You should do this for at least 2 floors.
Based on this FFT, what do you discern are the natural frequencies ωk (k = 1 − 4) at which
the building vibrates?
THEORETICAL CONSIDERATIONS
How does this compare to theory? To answer that, you must recall the halcyon days of matrix
math—eigenvalue problems applied to mechanical vibrations (It wasn’t so long ago was it?).
Compare and contrast your experimental and theoretical results.
Again, the main take home message is that even if your data looks really messy, there may be
some beautiful and very meaningful underlying structure to it, you just have to use the proper tool,
in this the FFT. File that away in your tricks-of-the-trade tool belt!
9
4
The Sound of Music–Choose your own adventure
Why does a violin have a different sound quality (timbre) than a piano? Put another way, if you
play an A on the piano, it sounds very differently than if you play an A on, say, the clarinet. Turns
out that our human ears and auditory system are very atuned to the precise recipe of harmonics
arriving at our ear drum. Our auditory system essentially performs a Fourier analysis, and what
we ultimately perceive in terms of sound quality depends on the how strongly each harmonic is
mixed in—-i.e, the relative magnitudes of the cn ’s in the Fourier seres.
Sounds waves are of course longitudinal pressure waves traveling through air. So, music is great
fodder for Fourier analysis.
Figure 7: Why does a piano sound different than a cell than a harp than a tuba? Image credit:
questgarden.com
10
4.0.1
THEORETICAL CONSIDERATIONS
To understand why, say a piano, has multiple modes excited when you strike a key causing a
hammer to hit a string inside the piano, let’s do a little Fourier by hand. Imagine you have a piano
string of length L. The hammer strikes the string at a distance d = L/8 from the end, causing it
to displace at that point a distance y(L/8) = h. Thus, the deformation of the string just as the
hammer strikes it is modeled as follows:
(
y(x) =
h
d
x
h
− L−d
for 0 ≤ x < d
(x − d) + h for d < x ≤ L
Plot the function y(x) (displacement of the string vs. distance down the string) for 0 ≤ x ≤ L,
then compute the Fourier series coefficients. Make a stem plot of the result. Which modes (harmonic
numbers) will be most strongly excited? Which are least excited? You can/should double check
your result makes sense by synthesizing a wave according to this recipe in the Falstad Fourier java
applet.
EXPERIMENT
Time to choose your own math-meets-music adventure! Pair up with a classmate to develop your
own little mini-project involving FFT analysis and musical instruments. Be creative and study a
musical aspect that is particularly interesting to you! If you are stuck (unlikely since most of you
are musically inclined!) consult the instructor for ideas. To help get the wheels turning, here a
few sample ideas. You could compare sound quality from various instruments (piano vs guitar).
Or compare sound quality on the different makes/models of the same instrument (e.g. an upright
piano vs a grand (your friend singing vs your own pipes); an acoustic guitar vs electric; different
playing styles on a bass or reed instrument; the same song by two different artists. Please run
your idea by the instructor for approval before plunging ahead.
For your report, describe what you studied, present and briefly discuss your main empirical
findings. You should definitely discuss what you heard in relation to how that manifested in the
frequency spectra.
Below are some very specific instructions about how to use computer software to record sound
files and analyze them in matlab.
1. Launch Audacity, plug in an external microphone (if needed; many modern day laptops come
with reasonably hi-fi mics built in). Record a data file using the big red record button. Save
the file as a .wav. Optionally, you may want to highlight and zoom in on the section of
interest. You’ll need to analyze only a single note or chord at a time, so something
like 100-400 msec worth of data should be plenty. Note that Audacity can play a highlighted
section only, such that you can verify you have captured a single note or chord being played
by the orchestra.
11
2. Export this cropped selection as a .wav format sound file for further analysis. To do this,
navigate to File >> Export Selection as WAV. Important: when saving the file, save to your
h: drive, or other network drive. It is strongly recommended to save this file into the folder
where all other Fourier series materials were originally unzipped.
3. For frequency analysis of the .wav file you will use MATLAB
4. Now, download the frequency spectra analysis script from the course webpage (it is a .m file).
Save this file to exactly the same folder where your .wav files are. Do NOT place it in a
subfolder/directory.
To test whether everything is working ok, type at the command prompt:
>> which freqAnalysis
You should see a directory/folder returned as text. Otherwise, if you get a message like
“freqAnalysis not found” something has gone wrong. Consult the instructor for help as
needed.
5. Now to use the function, you can type at the command prompt something like:
>> FFT = freqAnalysis(‘pianoE4.wav’, [0 3000]);
Note the single quote marks around the file name and the square brackets around 0 to 3000.
This command tells MATLAB: Please use the function freqAnalysis.m to analyze the audio
file pianoE4.wav, compute the Fourier transform, and display the results for all frequencies
between 0–3000 Hz. Of course you need to change the file name to whatever you named
your file. Also, you may want to change the frequency limits to something more conducive
for viewing, should you have mostly very low frequency content, or high frequency content.
Don’t forget the square brackets around 0 and 3000—they are essential! If all goes well, the
computer should think for a few seconds, then output a nice graph of the FFT, which you
can then analyze in detail. I highly recommend saving and/or printing out frequency spectra.
You’ll want them for discussion in your final report.
12
Appendix: Some Math Theory on Fourier Series and Transforms
Figure 8: Example of Fourier synthesis. Blue + green + red waves = brown wave. Image credit:
http://electron6.phys.utk.edu/optics421/modules/m5/Coherence.htm
Fourier synthesis means adding many waves, each with just the right amplitude, to obtain an
arbitrary periodic waveform. A simple example is shown in Figure 8. Mathematically, this idea
can be expressed as in Eqn. 1.
∞
∞
X
ao X
x(t) =
+
an cos(nωo t) + bn sin(nωt) =
cn ejnωo t
2
n=−∞
(1)
n=1
where x(t) is an abtirary periodic waveform, with fundamental period T = 2π/ωo . We’ve
derived/discussed in class, how to determine the an ’s, bn ’s, and cn ’s using orthogonality , where n
is the harmonic number . Determining these coefficients is often referred to as “doing a Fourier
analysis.”
Note that a Fourier Series is restricted to finding coefficients at discrete integer spacings of
ωn . The notion of a Fourier Transform extends the idea of a Fourier Series to add up waves
over a continuous spectrum of (angular) frequencies ω. This assingment focuses on using a computer implementation of the Fast Fourier Transform. All real data sets in the world are actually
discrete—we take samples of electrical or mechanical or optical quantities at discrete times. A good
experiment design will sample data so fast at a rate fs samples/s (Hz) such that the data look sort
of continuous. The upshot is that when a computer computes a Fourier Transform, it does so using
a famous (in the math world) algorithm known as the Discrete Fast Fourier Transform (FFT
for short).
13
Download