<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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