matlab like to discuss
@phi
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
This is the finished program. @phi like to discuss the while loop with you.
I would use a for loop. It's clearer.
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;
lpf, hpf, and bpf are vectors lpf and hpf are 2x5 matrices and bpf is a 2x9 matrix
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
Okay I got that. what does finalAudio = finalAudio + band1 do?
finalAudio = 0 so then finalAudio = 0 + band1
band1 is the output of the filter. it looks like you can selectively add in different bands
band1 is a vector. 0+vector will return a vector.
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)
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.
fileName = strcat(fileName, '.wav'); audiowrite(fileName, finalAudio, '.wav'); string = strcat('Successfully wrote ''', fileName, ''' to a file. \n'); What about this little bit of code?
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.
strcat extracts or attaches .wav?
appends (attaches) you can try it in the matlab command window to see the effects.
Then audiowrite turns the .wav into an audio file?
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.
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.
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.
Join our real-time social learning platform and learn together with your friends!