Ask your own question, for FREE!
Mathematics 8 Online
OpenStudy (raffle_snaffle):

matlab like to discuss

OpenStudy (raffle_snaffle):

@phi

OpenStudy (raffle_snaffle):

clear, clc, close all % clears all windows % loads rdd file into matlab [z, fs] = audioread('rdd.wav'); % loads filter coeffs data load('filter_coeffs'); % band 1 is lpf low frequences % band 2 is the bpf mid-range frequences % band 3 is the hpf high frequences % z matrix contains 2 channels sound sample vs time (left channel col1 and right channel col2) % asking user which channel to use, left is channel 1 and right is channel % 2 channel = input('Choose which channel you would like to filter(1 for left, 2 for right): '); if (channel == 1) fprintf('Left channel selected \n'); colChosen = z(:,1); elseif (channel == 2) fprintf('Right channel selected \n'); colChosen = z(:,2); end % asking user to select three of the bands given in file bands = input('Chose which of 3 bands you woud like in your filtered wav file: '); d = length(bands); m = 1; finalAudio = 0; while (m <= d) if (bands(m) == 1) fprintf('Including band 1. \n'); band1 = filter(lpf(2,:), lpf(1,:), colChosen); finalAudio = finalAudio + band1; m = m + 1; elseif (bands(m) == 2) fprintf('Including band 2. \n'); band2 = filter(bpf(2,:), bpf(1,:), colChosen); finalAudio = finalAudio + band2; m = m + 1; elseif (bands(m) == 3) fprintf('Including band 3. \n') band3 = filter(hpf(2,:), hpf(1,:), colChosen); finalAudio = finalAudio + band3; m = m + 1; end end % asking user if want to save filtered results saveFile = input('Would you like to save your filtered results to file(y/n)? ', 's'); if (saveFile == 'y' || saveFile == 'Y') fileName = input('Enter the file name (do not include the .wave extension):', 's'); fileName = strcat(fileName, '.wav'); audiowrite(fileName, finalAudio, '.wav'); string = strcat('Successfully wrote ''', fileName, ''' to a file. \n'); fprintf(string); else fprintf('Thank you for using this program. \n'); end

OpenStudy (raffle_snaffle):

This is the finished program. @phi like to discuss the while loop with you.

OpenStudy (phi):

I would use a for loop. It's clearer.

OpenStudy (raffle_snaffle):

what is the filter() function doing in this instance and how would I use a for loop? From the Fibonacci program we discussed last time I understand what the while loop is doing, but I don't understand in this case what the code inside the while loop is doing. d = length(bands); m = 1; finalAudio = 0; if (bands(m) == 1) fprintf('Including band 1. \n'); band1 = filter(lpf(2,:), lpf(1,:), colChosen); finalAudio = finalAudio + band1; m = m + 1; elseif (bands(m) == 2) fprintf('Including band 2. \n'); band2 = filter(bpf(2,:), bpf(1,:), colChosen); finalAudio = finalAudio + band2; m = m + 1; elseif (bands(m) == 3) fprintf('Including band 3. \n') band3 = filter(hpf(2,:), hpf(1,:), colChosen); finalAudio = finalAudio + band3; m = m + 1;

OpenStudy (raffle_snaffle):

lpf, hpf, and bpf are vectors lpf and hpf are 2x5 matrices and bpf is a 2x9 matrix

OpenStudy (phi):

the while loop starts with m=1 and loops until m=3 (I assume length of bands is 3) each time through the loop , it tests what value of m is and does the appropriate filter

OpenStudy (raffle_snaffle):

Okay I got that. what does finalAudio = finalAudio + band1 do?

OpenStudy (raffle_snaffle):

finalAudio = 0 so then finalAudio = 0 + band1

OpenStudy (phi):

band1 is the output of the filter. it looks like you can selectively add in different bands

OpenStudy (phi):

band1 is a vector. 0+vector will return a vector.

OpenStudy (phi):

logically, you don't need the while loop, *IF* you know the bands vector is "in order" It would be safer to make bands a vector of 1/0 and position (or index) represents if you include that band. then you don't need the while loop. example: if you want band1 and band3 you would set band to band= [1 0 1] and then test: if (band(m)==1)

OpenStudy (phi):

if on the other hand you really want to process the data in the order band= [3 2 1] where you first do band 3 then 2 then 1 they the while loop let's you do that.

OpenStudy (raffle_snaffle):

fileName = strcat(fileName, '.wav'); audiowrite(fileName, finalAudio, '.wav'); string = strcat('Successfully wrote ''', fileName, ''' to a file. \n'); What about this little bit of code?

OpenStudy (phi):

string concatenation appends a file extension .wav audio write must write out an audio file (.wav format. google to found what that is) I would think they should write status=audiowrite(fileName, finalAudio, '.wav'); and check what value status contains. (though do "help audiowrite" to find out if it returns an error message the string reports whether you were successful.

OpenStudy (raffle_snaffle):

strcat extracts or attaches .wav?

OpenStudy (phi):

appends (attaches) you can try it in the matlab command window to see the effects.

OpenStudy (raffle_snaffle):

Then audiowrite turns the .wav into an audio file?

OpenStudy (phi):

audiowrite takes a vector of numbers and writes them to a file, but in a specific format. Look up .wav file format to see what format that is.

OpenStudy (raffle_snaffle):

Read Complete Audio File Create a WAVE file from the example file handel.mat, and read the file back into MATLAB®. Create a WAVE (.wav) file in the current folder. load handel.mat filename = 'handel.wav'; audiowrite(filename,y,Fs); clear y Fs Read the data back into MATLAB using audioread. [y,Fs] = audioread('handel.wav'); Play the audio. sound(y,Fs); an example on matlabs site. I see what is going on here.

OpenStudy (raffle_snaffle):

Okay, well I need to work on some other things. I mostly got on because I noticed you were on and I wanted to discuss this with you. Thanks.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!