Playing rhythms in Matlab

Hello,
I am trying to create rapid sequences of notes - each note lasting as short as 0.125s in some cases - in order to play different rhythms in Matlab. However, Matlab is not playing the individual tones purely; rather, it makes several jerking/offset sounds during the duration of the individual tones. How can I get these these tones to play purely and fluidly?
I have pasted some of my code below. Thanks!
function defineVars()
global samplingFrequency numRhythmUnits
global pitchRhythm
global pitchCue pitchTarget numToneUnits numContextPitches
global numTrials numRhythms numTimeStamps numPitches
global numUnitsPerTone numRhythmsPerTrial
rhythmNum = 1;
toneUnitNum = 2;
numContextPitches = 4;
samplingFrequency = 2000;
pitchCue = 622;
pitchTarget = 698;
pitchRhythm = [415 440 466 493];
numRhythmUnits = 1;
numTrials = 16;
numRhythms = 4;
numTimeStamps = 32;
numPitches = 4;
numRhythmsPerTrial =27;
numUnitsPerTone = 1;
numToneUnits = numTimeStamps*numUnitsPerTone;
rhythmMatrix(1,:,:) = [1 1; 1 0; 1 1; 1 0; 1 0; 1 0; 1 1; 1 0; 1 1; 1 0; 0 1; 0 0; 1 1; 1 0; 1 0; 1 0; 1 1; 1 0; 1 0; 1 0; 1 1; 1 0; 1 1; 1 0; 1 1; 1 0; 1 1; 1 0; 1 1; 1 0; 1 1; 1 0; 0 1; 0 0]; % rhythm type, consisting of 32 timeStamps
trialParams = zeros(numTrials,2);
currTrialRhythm = 4;
juliansMatrix = zeros(numTrials, numRhythmsPerTrial, numToneUnits, 1);
counter = 0;
for i = 1:numTrials %16 trials in total, corresponding to the 4 % unique combinations of rhythmic unit type (4) and pitch type (4)
currTrial = i;
for l =1:numRhythmsPerTrial % (27)
currRhythmNum = l;
for m = 1:numTimeStamps % (32), converting rhythmMatrix to juliansMatrix % do for every trial
currTimeStamp = m;
for n= 1:numUnitsPerTone
currToneUnit = n;
counter = (currTimeStamp-1)*numUnitsPerTone + currToneUnit;
if n <numUnitsPerTone && rhythmMatrix(currTrialRhythm,currTimeStamp,1)==1 ||n == numUnitsPerTone && rhythmMatrix(currTrialRhythm,currTimeStamp,1)==1 && rhythmMatrix(currTrialRhythm,currTimeStamp,2)==1 juliansMatrix(currTrial,currRhythmNum,counter,1) = 1;
elseif n <numUnitsPerTone && rhythmMatrix(currTrialRhythm,currTimeStamp,1) == 0 || n == numUnitsPerTone && rhythmMatrix(currTrialRhythm,currTimeStamp,2)==0
juliansMatrix(currTrial,currRhythmNum,counter,1) = 0;
end
end
end
end
currTrial = currTrial + 1;
trialParams(currTrial,rhythmNum) = currRhythmNum;
trialParams(currTrial,toneUnitNum) = counter;
end
for i =1:numRhythmsPerTrial
for o = 1:numToneUnits
currToneUnit = o;
if juliansMatrix(numTrials,numRhythmsPerTrial,currToneUnit,1) == 1
Fs = samplingFrequency; % Samples per second
toneFreq = pitchRhythm(1);
nSeconds = 0.125; % Duration of the sound
y = sin(linspace(0,nSeconds*toneFreq*2*pi,round(nSeconds*Fs)));
sound(y,Fs); % Play sound at sampling rate Fs
else
Fs = samplingFrequency; % Samples per second
toneFreq = 0;
nSeconds = 0.125; % Duration of the sound
y = sin(linspace(0,nSeconds*toneFreq*2*pi,round(nSeconds*Fs)));
sound(y,Fs); % Play sound at sampling rate Fs
end
end
end
end

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 20 May 2021

0 votes

There are a couple small but crucial flaws in your generated audio signal.
(1) There is an index related err.
(2) Your generated signal tones are not well aligned with the final sound signal subject for display.

Tags

Asked:

on 21 Feb 2011

Community Treasure Hunt

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

Start Hunting!