Generating a sequence of tones

I am trying to generate a sequences of tones (of a standard frequency and duration) separated by a standard interval. For example, a series of 5 tones (1000 Hz, 1 sec.) with an interval of 2 seconds between the onset of each tone. What is the best way to go about this?

 Accepted Answer

This works:
Fs = 8192; % Default Sampling Frequency (Hz)
Ts = 1/Fs; % Sampling Interval (s)
T = 0:Ts:(Fs*Ts); % One Second
Frq = 1000; % Tone Frequency
Y = sin(2*pi*Frq*T); % Tone
Y0 = zeros(1, Fs*2); % Silent Interval
Ys = [repmat([Y Y0], 1, 4) Y]; % Full Tone With Silent Intervals
soundsc(Ys,Fs); % Play Sound

6 Comments

Thanks for your response! I'm getting an error message after: Ys=[repmat([Y Y0],1,4)Y]; It says "Unexpected Matlab expression". Any idea what I might be doing wrong here?
My pleasure!
It could be version problems, since it worked perfectly for me in R2014b. I don’t remember when repmat was introduced.
If all else fails, just brute-force it:
Ys = [Y Y0 Y Y0 Y Y0 Y Y0 Y];
It worked! I'm working off of 2014a (Student version) , that must have been the issue. Thanks again!
My pleasure!
I’m glad it worked. The repmat glitch still mystifies me.
repmat() has been around for hundreds of years, if not millennia. He's missing a space or comma after the right parenthesis in
Ys=[repmat([Y Y0],1,4)Y];
Star Strider
Star Strider on 27 Jan 2015
Edited: Star Strider on 27 Jan 2015
I would agree, except that my original code (as posted, with the space, and correctly formatted in my Answer) worked without error when I ran it. I always test my code if possible before I post it, or clearly label it as untested. Perhaps a comma would have made it more robust, but it works as posted. Copy, paste, run.

Sign in to comment.

More Answers (0)

Asked:

on 26 Jan 2015

Edited:

on 27 Jan 2015

Community Treasure Hunt

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

Start Hunting!