Clear Filters
Clear Filters

How to generate tones with varying F0, F2, duration and risetime

1 view (last 30 days)
Hello everyone! I'm trying to generate a range of stimuli varying across different acoustic characteristics: F0, F2, duration and risetime. I managed to put together changes across F0, F2, and duration, but the script fails when I add the additional loop to generate changes in risetime (I marked those added lines as %new in my script). Is there anything I'm missing here why these sounds cannot be generated? Any suggestions/hints would be super appreciated.
clear all
F0s = [100 100*2^(3/12)];
F2s = [2040 2040*2^(6/12)];
durs = [0.100 0.325];
RTms = [0.015 0.150];
fs = 44100;
%amplitude_rampup =
%sin(0:(pi/2)/floor(0.010*fs):pi/2-((pi/2)/floor(0.010*fs))).^2; %replaced
%amplitude_rampdown =
%sin(pi/2:(pi/2)/ceil(0.010*fs):pi-((pi/2)/ceil(0.010*fs))).^2; %replaced
for n = 1:length(F0s)
for m = 1:length(F2s)
for p = 1:length(durs)
for rd = 1:length(RTms) %new
F0 = F0s(n);
F2 = F2s(m);
dur = durs(p);
rtm = RTms(rd); %new
ramp_dur = RTms(rd); %new
ramp = 0:1/ceil(ramp_dur*fs):1; %new
amplitude_rampup = ramp; %new
amplitude_rampdown = fliplr(ramp); %new
amplitude_mod = [amplitude_rampup ones(1, fix(dur*fs)-2*length(amplitude_rampup)) amplitude_rampdown]; %added fix to force the result to integer format
time_points = 1/fs:1/fs:dur;
pitch_contour = [];
pitch_contour(1:length(time_points)) = F0;
base_stim = [];
base_stim(1:length(time_points)) = 0;
F = [F2];
BW = [100];
R = exp(-pi*BW/fs); % Pole radii
theta = 2*pi*F/fs; % Pole angles
poles = R .* exp(j*theta); % Complex poles
B = 1; A = real(poly([poles,conj(poles)]));
for q = 1:40
previous_phase = 0;
for r = 1:length(pitch_contour)
if pitch_contour(r) > 0
base_stim(r) = base_stim(r) + 0.5 * sin(2 * pi * pitch_contour(r) * q * (1/fs) + previous_phase);
previous_phase = mod(2*pi*pitch_contour(r) * q * (1/fs) + previous_phase,2*pi);
end
end
end
base_stim = filter(B, A, base_stim);
base_stim = base_stim.*amplitude_mod;
base_stim = (base_stim ./ max(abs(base_stim)));
base_stim = base_stim.*0.9;
audiowrite(['F0' num2str(F0) '_F2' num2str(F2) '_dur' num2str(dur) 'risetime' num2str(rtm) '.flac'],base_stim,fs);
end %new
end
end
end
  1 Comment
Geoff Hayes
Geoff Hayes on 22 Sep 2021
Magdalena - when I run your code, I see the following error
Arrays have incompatible sizes for this operation.
Error in XX (line YY)
base_stim = base_stim.*amplitude_mod;
because the dimensions of base_stim and amplitude_mod are different: 1x4410 vs 1x13232. This occurs on the second iteration of the innermost loop when rd is 2 and ramp_dur is 0.15. You will want to take this into account when determing base_stim.

Sign in to comment.

Answers (0)

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!