Clear Filters
Clear Filters

write out separate files for each iteration

1 view (last 30 days)
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
  1 Comment
frankenberry
frankenberry on 29 Jul 2017
Thank you for your response. I'm so sorry that this question wasted your time. My browser crashed before I submitted it so I'm not sure why it was submitted. I was able to figure this out a few days ago - the same day I submitted it actually. I thought when my browser crashed that the question was lost. I am so sorry.

Sign in to comment.

Accepted Answer

Nirja Mehta
Nirja Mehta on 28 Jul 2017
Are you getting any error? If yes, can you provide your .wav files?
  1 Comment
frankenberry
frankenberry on 29 Jul 2017
I responded. Thank you very much for your help. Are you any good at clicktrains? I will paste my m-file below for what I have so far. I'm stuck on how to get the clicks to present as follows: click1 ---75ms-- click1 --4ms-- click2 ---75ms--- click1 --4ms-- click2 --4 ms-- click3 --- 75ms ---(train repeats)click1 --- 75 ms ---click1 --4ms-- click2 --4ms....We use an array of zeros and then enter 1s for the clicks. Each click has to be 100 microseconds.
m file so far: pth = 'C:\MATLAB\Sound_wavs\ABR';
clear, clc; ici = 4; %inter-click interval for 2 or more clicks Fs = 32e3; % sampling frequency soa = 75; %time between click groups dur = 1000; rate = (dur/soa); %frequency of the impulse in Hz is 13.3 clicklength = (Fs/dur/Fs); %click length is equal to 100 microseconds (i.e. 0.001 ms)
num_samples = (Fs/rate); %number of samples?? % click=1(0:soa:length(holder)); %create click ABR holder = zeros(Fs,2); %two columns of 32e3 zeros click = ones(1,2); %one ms click in two columns
for j = 0:length(holder); holder(0:soa:end,:) = [click];
%For ti=1:soa:Fs filenam = ['C:\MATLAB\Sound_wavs\ABR\' 'Clicks' soa_ici '_32e3.wav']; audiowrite(filenam,Fs); [clicks,Fs]=audioread('C:\MATLAB\Sound_wavs\ABR' 'Clicks' soa_ici '_32e3.wav'); soundsc(click,Fs);
plot(ti,clicks);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!