<< Chapter < Page Chapter >> Page >

Another approach would be to find the center of mass of the data. This decision rule is more comprehensive because it takes into account all data from the Fourier transform, not just the maximum value. In order to find the average weight of all amplitudes, we change the inner part of code to the following (starting with "%find the maximum one"):

... %find the frequency corresponding to the "average" amplitudeavg_freq = sum(freq.*amps)/(sum(amps)*df);%decided which way the car should move based on the max frequency if avg_freq<freq_criterion; ...

One can imagine myriad other ways to approach this problem. Many strategies have been developed, but the question is open-ended. A natural next step is for the reader to think of new ways to interpret spectrogram data. The most effective characterizations probably have yet to be discovered!

Conclusion

In this module we developed the tools to decompose an arbitrary signal, such as an EEG, into is component frequencies. We began with sine waves, established the trapezoid scheme, and finally introduced Fourier analysis. This same flavor of analysis is used in many other settings, too–see the related documents.

Code

Code for mytrapz.m

function curve_area = mytrapz(x, y, fast) % function curve_area = mytrapz(x, y, fast)% % mytrapz.m performs the trapezoid rule on the vector given by x and y.% % Input:% x - a vector containing the domain of the function % y - a vector containing values of the function corresponding to the% values in 'x' if nargin<3 curve_area = 0;%loop through and add up trapezoids for as many points as we are givenfor n = 2 : numel(x) height = (y(n) + y(n-1))/2; %average height of function across intervalbase = x(n) - x(n-1); %length of interval trap_area = base * height; %area of trapezoidcurve_area = curve_area + trap_area; %add to continuing sum endelseif fast%alternate (fast) implementation xvals = x(3:end) - x(1:end-2);yvals = y(2:end-1); curve_area = yvals(:)'*xvals(:);curve_area = curve_area + y(1)*(x(2) - x(1)) + y(end)*(x(end) - x(end-1)); curve_area = curve_area/2;end

Code for myfreq.m

% myfreq.m %% find the frequencies and amplitudes at which a wave is "vibrating" %% Contrast simple (but laborious) trapezoid computations to the fast % and flexible built-in fft command (fft stands for fast Fourier% transform). % To make full sense of this we will need to think about complex% numbers and the complex exponential function. %T = 5;% duration of signal dt = 0.001; % time between signal samplest = 0:dt:T; N = length(t);y = 2.5*sin(3*2*pi*t) - 4.2*sin(4*2*pi*t); % a 2-piece wave plot(t,y)xlabel('time (seconds)') ylabel('signal')for f = 1:5, % compute the amplitudes as ratios of areas a(f) = trapz(t,y.*sin(f*2*pi*t))/trapz(t,sin(f*2*pi*t).^2);end figureplot(1:5,a,'ko') % plot the amplitudes vs frequency hold onplot(1:5, [0 0 2.5 -4.2 0], 'b*')figure(34) f = (0:N-1)/T;% fft frequenciessc = N*trapz(t,sin(2*pi*t).^2)/T; % fft scale factor A = fft(y);newa = -imag(A)/sc; plot(f,newa,'r+')y = y + 3*cos(6*2*pi*t); % add a cosine piece figure(1)hold on plot(t,y,'g') % plot ithold off legend('2 sines','2 sines and 1 cosine')figure(2) A = fft(y); % take the fft of the new signalnewa = -imag(A)/sc; plot(f,newa,'gx')b = real(A)/sc; plot(f,b,'gx')xlim([0 7]) % focus in on the low frequencieshold off xlabel('frequency (Hz)')ylabel('amplitude') legend('by hand','by fft','with cosine')

Code for myfourier.m

% function [mag freq] = myfourier(y, dt, use_fft)% % myfourier.m decomposes the signal 'y', taken with sample interval dt,% into its component frequencies. %% Input: %% y -- signal vection % dt -- sample interval (s/sample) of y% use_fft -- if designated, use matlab's fft instead of trapezoid method %% Output: %% freq -- frequency domain % mag -- magnitude of frequency components of y corresponding to 'freq'function [freq mag] = myfourier(y, dt, use_fft)y = y(:); N = numel(y); %number of samplesT = N*dt; %total time t = linspace(0,T,N)'; %reconstruct time vectorhalf_N = floor(N/2); %ensures that N/2 is an integer if mod(N,2) %treat differently if f odd or evenfreq = (-half_N:half_N)'/T; %fft frequencies elsefreq = (-half_N:half_N-1)'/T; %fft frequencies endif nargin<3 %perform explicit Fourier transform sinmag = zeros(size(freq)); %vector for component magnitudescosmag = zeros(size(freq)); %vector for component magnitudes%loop through each frequency we will test for n = 1 : numel(freq)%obtain coefficient for freqency 'freq(n)' sinmag(n) = mytrapz(t, y.*sin(freq(n)*2*pi*t), 1);cosmag(n) = mytrapz(t, y.*cos(freq(n)*2*pi*t), 1); end%scale to account for sample lengthscale_factor = mytrapz(t, sin(2*pi*t).^2); sinmag = sinmag / scale_factor;cosmag = cosmag / scale_factor; mag = [sinmag(:) cosmag(:)];elseif use_fft %use built-in MATLAB fft() for speed fft_scale_factor = mytrapz(t, sin(2*pi*t).^2) * N / T;A = fft(y); mag(:,1) = -imag(A)/fft_scale_factor;mag(:,2) = real(A)/fft_scale_factor; mag = circshift(mag, half_N);end

Code for mysgram.m

% % function [stft_plot freq tm]= my_stft(y, dt, Nwindow) %% my_stft splits the signa 'y' into time windows, the breaks each % segment into its component frequencies. See "Short-time Fourier Transform"% %% Input: % y -- signal% dt -- sample interval % Nwindow -- number of time intervals to analyze% % Output:% stft_plot -- values plotted in the spectrogram % freq -- frequency domain% tm -- time domain function [stft_plot freq tm hh]= mysgram(y, dt, Nwindow) %count the number of windowsN = numel(y); win_len = floor(N/Nwindow);sm = zeros(win_len, Nwindow); cm = zeros(win_len, Nwindow);tm = linspace(0, numel(y) * dt, Nwindow); %for each windowfor n = 1:Nwindow %isolate the part of the signal we want to deal withsig_win = y((n-1)*win_len + 1 : n*win_len); %perform the fourier transform[freq mg] = myfourier(sig_win, dt, 1);sm(:,n) = mg(1:win_len,1); cm(:,n) = mg(1:win_len,2);end stft_plot = abs(sm + cm);stft_plot = stft_plot(end/2:end, :); %plot the fourier transform over timehh = imagesc(tm, freq(round(end/2):end), stft_plot); title('Spectrogram', 'FontSize', 20)xlabel('time', 'FontSize', 16) ylabel('frequency', 'FontSize', 16)set(gca, 'ydir', 'normal') %just look at lower frequenciesylim([0-win_len/2 50+win_len/2])

Questions & Answers

Discuss the differences between taste and flavor, including how other sensory inputs contribute to our  perception of flavor.
John Reply
taste refers to your understanding of the flavor . while flavor one The other hand is refers to sort of just a blend things.
Faith
While taste primarily relies on our taste buds, flavor involves a complex interplay between taste and aroma
Kamara
which drugs can we use for ulcers
Ummi Reply
omeprazole
Kamara
what
Renee
what is this
Renee
is a drug
Kamara
of anti-ulcer
Kamara
Omeprazole Cimetidine / Tagament For the complicated once ulcer - kit
Patrick
what is the function of lymphatic system
Nency Reply
Not really sure
Eli
to drain extracellular fluid all over the body.
asegid
The lymphatic system plays several crucial roles in the human body, functioning as a key component of the immune system and contributing to the maintenance of fluid balance. Its main functions include: 1. Immune Response: The lymphatic system produces and transports lymphocytes, which are a type of
asegid
to transport fluids fats proteins and lymphocytes to the blood stream as lymph
Adama
what is anatomy
Oyindarmola Reply
Anatomy is the identification and description of the structures of living things
Kamara
what's the difference between anatomy and physiology
Oyerinde Reply
Anatomy is the study of the structure of the body, while physiology is the study of the function of the body. Anatomy looks at the body's organs and systems, while physiology looks at how those organs and systems work together to keep the body functioning.
AI-Robot
what is enzymes all about?
Mohammed Reply
Enzymes are proteins that help speed up chemical reactions in our bodies. Enzymes are essential for digestion, liver function and much more. Too much or too little of a certain enzyme can cause health problems
Kamara
yes
Prince
how does the stomach protect itself from the damaging effects of HCl
Wulku Reply
little girl okay how does the stomach protect itself from the damaging effect of HCL
Wulku
it is because of the enzyme that the stomach produce that help the stomach from the damaging effect of HCL
Kamara
function of digestive system
Ali Reply
function of digestive
Ali
the diagram of the lungs
Adaeze Reply
what is the normal body temperature
Diya Reply
37 degrees selcius
Xolo
37°c
Stephanie
please why 37 degree selcius normal temperature
Mark
36.5
Simon
37°c
Iyogho
the normal temperature is 37°c or 98.6 °Fahrenheit is important for maintaining the homeostasis in the body the body regular this temperature through the process called thermoregulation which involves brain skin muscle and other organ working together to maintain stable internal temperature
Stephanie
37A c
Wulku
what is anaemia
Diya Reply
anaemia is the decrease in RBC count hemoglobin count and PVC count
Eniola
what is the pH of the vagina
Diya Reply
how does Lysin attack pathogens
Diya
acid
Mary
I information on anatomy position and digestive system and there enzyme
Elisha Reply
anatomy of the female external genitalia
Muhammad Reply
Organ Systems Of The Human Body (Continued) Organ Systems Of The Human Body (Continued)
Theophilus Reply
what's lochia albra
Kizito
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, The art of the pfug. OpenStax CNX. Jun 05, 2013 Download for free at http://cnx.org/content/col10523/1.34
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'The art of the pfug' conversation and receive update notifications?

Ask