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


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 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 we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?