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

what is a good calculator for all algebra; would a Casio fx 260 work with all algebra equations? please name the cheapest, thanks.
Kevin Reply
a perfect square v²+2v+_
Dearan Reply
kkk nice
Abdirahman Reply
algebra 2 Inequalities:If equation 2 = 0 it is an open set?
Kim Reply
or infinite solutions?
Embra Reply
if |A| not equal to 0 and order of A is n prove that adj (adj A = |A|
Nancy Reply
rolling four fair dice and getting an even number an all four dice
ramon Reply
Kristine 2*2*2=8
Bridget Reply
Differences Between Laspeyres and Paasche Indices
Emedobi Reply
No. 7x -4y is simplified from 4x + (3y + 3x) -7y
Mary Reply
is it 3×y ?
Joan Reply
J, combine like terms 7x-4y
Bridget Reply
im not good at math so would this help me
Rachael Reply
how did I we'll learn this
Noor Reply
f(x)= 2|x+5| find f(-6)
Prince Reply
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
Crystal Reply
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Chris Reply
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
what is system testing
can nanotechnology change the direction of the face of the world
Prasenjit Reply
At high concentrations (>0.01 M), the relation between absorptivity coefficient and absorbance is no longer linear. This is due to the electrostatic interactions between the quantum dots in close proximity. If the concentration of the solution is high, another effect that is seen is the scattering of light from the large number of quantum dots. This assumption only works at low concentrations of the analyte. Presence of stray light.
Ali Reply
the Beer law works very well for dilute solutions but fails for very high concentrations. why?
bamidele Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.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?