MATLAB: Write out separate files for each iteration

adding waveformscarrier frequencymodulation depthmodulation frequency

I have two m files. In the first m files I create several wav files. Each wav file has a carrier frequency, modulation rate and modulation depth. There are 4 carrier frequencies (CF), 4 modulation frequency(MF), and 5 modulation depths (MD). I want to sum the carriers at the same modulation depth and write out each file separately. Example filenames are MF104_CF416_MD0_24, MF107_CF749_MD0_24, MF111_CF1332_MD0_24, MF113_CF2034_MD0_24 would be written out to one file, then the next modulation depth would have the same format but with a different MD#. I also need to plot the fft, the modulation envelope, and the modulated signal.
This is the code I have so far (I can't figure out what I'm doing wrong, if you respond, please do not use the 'squeeze' command): clc; pth = 'C:\MATLAB\Sound_wavs\For_Experiment\'; Fs=32e3; stim_mods = [104 107 111 113]; stim_cars = [416 749 1332 2034]; mod_step = -3; mod_final = -12; modulations = {'0', '-3', '-6', '-9', '-12'}; dur=1000;
% time index ti = 1/Fs:1/Fs:(dur/1000);
mod_num = (mod_final/mod_step)+1; %iteration for modulation depth sig = zeros(Fs,2,length(stim_mods),mod_num); %pre-allocated zero array for modulations
% Read the all wav files in the pth folder, iterate through mod_depth to find stim_name for stim = 1:length(stim_mods) stim_mod_str = num2str(stim_mods(stim)); stim_car_str = num2str(stim_cars(stim));
mod_depth_counter = 0;
for mod_depth = 0:mod_step:mod_final
mod_depth_counter = mod_depth_counter+1;
mod_depth_str = num2str(mod_depth);
stim_name = ['C:\MATLAB\Sound_wavs\For_Experiment\' 'MF' stim_mod_str '_CF' stim_car_str '_MD' mod_depth_str '_24.wav'];
[sigout(:,:,stim,mod_depth_counter),Fs]=audioread(stim_name);
% sig(:,:,stim, mod_depth_counter) = sigout;
end
end
% Add the wav files with the same modulation depths together; % change the 1 in soundout for the next grouped sound for mod_depth=1:length(mod_num) sig = sum(sigout,3); sound(sig(:,:,:,5)); %to hear the different mod_depths, change value from 1-5 end
% Normalize the added wav files; norm_stim = zeros(Fs,2); mod_num_str = num2str(mod_num); for channels = 1:2 norm_stim(:,channels) = sig(:,channels,1).*10^(-16/20); end
% Write out the new wav file – i.e. all cf=416,749,1032,2034 at one mod depth in one file stim_name_new = ['C:\MATLAB\Sound_wavs\For_Experiment\' 'All_MF_' mod_depth_str '_24.wav']; stim_out = audiowrite(stim_name_new,norm_stim,Fs,'BitsPerSample',24);
% Visualize signal nfft = Fs; nyq = Fs/2; fi = 0:nyq;
f = fft(norm_stim(:,1),nfft); fAmp = (2/nfft)*abs(f); h = abs(hilbert(norm_stim(:,1))); fh = (2/nfft)*abs(fft(h,nfft)); hold on;
figure;
% Plotting the FFT spectrum and modulation frequency subplot(221) %subplot(1,5,i); stairs(fi(1:2501),fAmp(1:2501),'k'); title({'FFT of Spectral Envelope'; 'and Modulation Frequency'}); xlabel('Frequency (Hz)'); ylabel('Modulation Depth'); axis([20 2500 0 0.5]); hold on stairs(fi(1:2501),fh(1:2501),':r'); legend('Signal','Modulation Frequency'); hold off;
% Plotting the modulation envelope subplot(222); plot(norm_stim(1:length(norm_stim)/100),'k'); title('Modulation Envelope'); xlabel('Time (s)'); ylabel('Modulation Depth'); axis([0 300 -0.5 0.5]);
subplot(2,2,[3,4]); plot(norm_stim(1:length(norm_stim)/10),'k'); title(['Modulated Signal at MD_' mod_depth_str '.wav' ]); xlabel('Time (seconds)'); ylabel('Modulation Depth'); axis([0 2500 -0.5 0.5]);
%If sound needs to be adjusted, then use the adjustment factor for decibels %Recall 20*log10(.5)= -6.0206 %sig*(10^(-dB/20)) will turn down the sound by xdB

Best Answer

Are you getting any error? If yes, can you provide your .wav files?
Related Question