Function to play sequential tones with frequencies as input arguments

I am new to MATLAB and trying to write a function that produces multiple tones and plays them through the soundcard sequentially, with an interstimulus interval (ISI) between each tone. For input arguments, I want the function to accept the frequency of tones to be played (so tones(500,1000,1500) would play 3 tones at the three respective frequencies).
I am new, so don't judge, but thus far I have:
sequence=[];
for k=1:length(nargin)
if nargin==0;disp('Error! Please enter at least one frequency.');
elseif nargin>0;freq=varargin{1:length(nargin)};end;
sr=44100;
dur=0.5; %tone duration is 0.5 sec
t=0:1/sr:dur;
y=sin(2*pi*freq*t);
isiDur=1.5; %ISI duration is 1sec+tone duration because they play simultaneously
isiT=0:1/sr:isiDur; %ISI will now play for 1sec after tone
isi=sin(0*isiT);
sequence=[sequence;y(:)];
end;
sound(sequence,sr)
If I play sound(y,sr) sound(isi) then I can get one tone and ISI. But, I can't get sequential tones regardless of the ISI aspect. Please help!

 Accepted Answer

Morgan - I'm guessing that the above code is the body of a function that has a variable number of inputs...that is why you are using nargin and varargin (your function may even be named tones?).
nargin indicates the number of input parameters/arguments to your function. Since it is an integer, it will be represented by a 1x1 scalar. So the
length(nargin)
will always be one, which isn't what you want. So you need to change your code slightly so that you are able to access each input parameter (and not just the first one)
function tones(varargin)
if nargin==0
error('Error! Please enter at least one frequency.');
end
sequence=[];
for k=1:nargin
freq=varargin{k};
sr=44100;
% etc.
end
The above isn't all that different from your code, it just does the nargin==0 check outside of the loop, and uses the error function if this is true. We then iterate over each input argument (frequency) and do as you did before. Try the above and see what happens!

5 Comments

That makes so much sense! Thank you.
I can now play several tones in a row, but can't figure out how to do an ISI after each tone.
I was thinking of creating a cell array for my tone and for the ISI and adding them together, and then using the loop to play my tones according to the input parameters. But, I get the following error:
??? Undefined function or method 'min' for input arguments of type 'cell'.
Error in ==> sound at 46
y = max(-1,min(y,1));
Maybe cell arrays aren't the way to go. How else can I incorporate an ISI after each tone?
I have also tried using pause(n), but can only get a pause before or after the entire sequence of tones play, not in-between each tone.
Morgan - is the ISI just that which "appears" between the end of the first tone and the beginning of the second? Or does it and the tone start at the same time with the former continuing for after the latter has finished (since the ISI has a duration of 1.5 seconds vs that for the tone's 0.5 seconds)?
If the latter, then I suppose you could combine/add them as
for k=1:nargin
freq=varargin{k};
sr=44100;
% etc.
% initialize combined as isi
combined = isi;
% add the tone to the combined
combined(1:length(y)) = combined(1:length(y)) + y;
sequence=[sequence; combined(:)];
end
Yes! That did it! Thank you so much for your help and explanations, Geoff!

Sign in to comment.

More Answers (0)

Tags

Asked:

on 10 Nov 2014

Commented:

on 11 Nov 2014

Community Treasure Hunt

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

Start Hunting!