Explore BrainMass

# MATLAB program loop

Not what you're looking for? Search our solutions OR ask your own Custom question.

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

Please type something up step by step how this program work, maybe some pictures if possible showing how the code changes. The comments are good, but does not explain how the code really works....Like you type project5.wav on command window, it is assigned to filename, then the next line ... pictures or a number table or something to see how this works. I am lost. For reference, project5.wav was a file that had a plot for numbers say 5,6,7,8,9 converted into a dialtone signal. This program takes that plot, using ginput function you click on the waveforem and the program separates it into an x component and an fs component....I think. Then you plot the signl versus the spectrum

Problem, I asked for help with implementing a for loop and got help using a switch statement. I need a for loop so when you say do 8 clicks, 10 clicks, to infinity if want the program will plot all 8 clicks, 10 clicks, etc. I need the for loop for the plot and sublot sections to take the number of clicks, any number, and then the program using the for loop generate the number of signal and spectrum plots for each click. I am bad a t for loop especially when you have to plot something over and over from 0 to nth terms. Here is the program. Also, each signal t1,t2,etc. is calculated up to t5. The original program only asked for 6 clicks. Now need the program for nth clicks as said. So this area will need to be in a for loop too so when ginput function sees say 20 clicks then the for loop says do 1 to 20 signals and plot them, but can only have 5 pairs of plots(signal, spectrum) per page so total 10 plots per page.
_________________________________________________________________

%tones, read file and plot the file. Use ginput to take 6 data points.

%Plot waveforms.

clear,clc,close all;

%get project5.wav file from moodle, right click, save and drag to m file

%directory used in matlab so matlab can find it.

filename=input('Enter wavfilname: "project5.wav" > ','s');

%enter at prompt>> project5.wav

%-------------------------------------------------------------------------

t=(0:length(wav)-1)/fs;

plot(t,wav);

[x,y]=ginput(); % 6 means 6 mouse clicks only to work on plot

%Be sure to click on 6 different places on plot

%cause if plot on same waveform("square wave")

%twice the final plots are messed up.

%-------------------------------------------------------------------------

%then on command window see x = a number

% a number, etc.

% y = a number

% a number etc.

%but actually do not cause added a ";" to code to

%supress all the numbers being placed on the

%command window.

%only want x axis, so ignore y axis and to do so..

%--------------------------------------------------------------------------

index=round(x*fs);

figure; %creates a new figure window

t1=t(index(1):index(2)-1); % elements from t[index(1)] to t[index(2)-1] are saved in array 't1'

sig1=wav(index(1):index(2)-1); %elements from t[index(1)] to t[index(2)-1] are saved in array 'sig1'

N=length(t1); %take clicked portion length

%FFT(sig1) is the discrete Fourier transform (DFT) of vector sig1
% fftshift(fft(sig1)) Shift zero-frequency component to center of
% spectrum 'fft(sig1)'
%abs(fftshift(fft(sig1))) is absolute value of 'fftshift(fft(sig1))'
fft1=abs(fftshift(fft(sig1)))/N; %get spectrum

% scaller multiplication of fs/N by the vector [-N/2, (-N/2+1), ... , -1,
% 0, 1, ..., (N/2-1), N/2]
freq1=[-N/2:N/2-1]'*fs/N;

%elements from t[index(2)] to t[index(3)-1] are saved in array 't2'
t2=t(index(2):index(3)-1);

%elements from wav[index(2)] to wav[index(3)-1] are saved in array 'sig2'
sig2=wav(index(2):index(3)-1);

%opens a plot matrix of 5 by 2 and puts in place 2; and plots sig2
subplot(5,2,2);plot(sig2);

%finds lenght of t2
N=length(t2);

%FFT(sig2) is the discrete Fourier transform (DFT) of vector sig2
% fftshift(fft(sig2)) Shift zero-frequency component to center of
% spectrum 'fft(sig2)'
%abs(fftshift(fft(sig2))) is absolute value of 'fftshift(fft(sig2))'

fft2=abs(fftshift(fft(sig2)))/N;

% scaller multiplication of fs/N by the vectore [-N/2, (-N/2+1), ... , -1,
% 0, 1, ..., (N/2-1), N/2]
freq2=[-N/2:N/2-1]'*fs/N;

%elements from t[index(3)] to t[index(4)-1] are saved in array 't3'
t3=t(index(3):index(4)-1);

%elements from wav[index(3)] to wav[index(4)-1] are saved in array 'sig3'
sig3=wav(index(3):index(4)-1);

%in place 3 of plot matrix of 5 by 2 plots sig3
subplot(5,2,3);

plot(sig3);

%finds the lenght of t3
N=length(t3);

%FFT(sig3) is the discrete Fourier transform (DFT) of vector sig3
% fftshift(fft(sig3)) Shift zero-frequency component to center of
% spectrum 'fft(sig3)'
%abs(fftshift(fft(sig3))) is absolute value of 'fftshift(fft(sig3))'
fft3=abs(fftshift(fft(sig3)))/N;

% scaller multiplication of fs/N by the vectore [-N/2, (-N/2+1), ... , -1,
% 0, 1, ..., (N/2-1), N/2]
freq3=[-N/2:N/2-1]'*fs/N;

%elements from t[index(4)] to t[index(5)-1] are saved in array 't4'
t4=t(index(4):index(5)-1);

%elements from wav[index(4)] to wav[index(5)-1] are saved in array 'sig4'
sig4=wav(index(4):index(5)-1);

%in place 4 of plot matrix of 5 by 2 plots sig4
subplot(5,2,4);

plot(sig4);

%finds the lenght of t4
N=length(t4);

%FFT(sig4) is the discrete Fourier transform (DFT) of vector sig3
% fftshift(fft(sig4)) Shift zero-frequency component to center of
% spectrum 'fft(sig4)'
%abs(fftshift(fft(sig4))) is absolute value of 'fftshift(fft(sig4))'
fft4=abs(fftshift(fft(sig4)))/N;

% scaller multiplication of fs/N by the vectore [-N/2, (-N/2+1), ... , -1,
% 0, 1, ..., (N/2-1), N/2]
freq4=[-N/2:N/2-1]'*fs/N;

%elements from t[index(5)] to t[index(6)-1] are saved in array 't5'
t5=t(index(5):index(6));

%elements from wav[index(5)] to wav[index(6)-1] are saved in array 'sig5'
sig5=wav(index(5):index(6));

%in place 4 of plot matrix of 5 by 2 plots sig5
subplot(5,2,5);
plot(sig5);

%finds the lenght of t5
N=length(t5);

%FFT(sig5) is the discrete Fourier transform (DFT) of vector sig5
% fftshift(fft(sig5)) Shift zero-frequency component to center of
% spectrum 'fft(sig5)'
%abs(fftshift(fft(sig5))) is absolute value of 'fftshift(fft(sig5))'
fft5=abs(fftshift(fft(sig5)))/N;

% scaller multiplication of fs/N by the vectore [-N/2, (-N/2+1), ... , -1,
% 0, 1, ..., (N/2-1), N/2]
freq5=[-N/2:N/2-1]'*fs/N;

no = INPUT('Enter the i for ith plot(1 < i < 10)','s') ;
no=str2num(no);

switch no
case 1,
subplot(5,2,1);plot(t1,sig1);
subplot(5,2,2);plot(freq1,fft1);
case 2,
subplot(5,2,3);plot(t2,sig2);
subplot(5,2,4);plot(freq2,fft2);
case 3,
subplot(5,2,5);plot(t3,sig3);
subplot(5,2,6);plot(freq3,fft3);
case 4,
subplot(5,2,7);plot(t4,sig4);
subplot(5,2,8);plot(freq4,fft4);
case 5,
subplot(5,2,9);plot(t5,sig5);
subplot(5,2,10);plot(freq5,fft5);
end