Generate Sound for a range of frequencies in MATLAB individually

70 views (last 30 days)
I wanted to generate sound from MATLAB for frequencies between 1Hz and30kHz. Technically, I shouldn't be able to hear sounds which are 30kHz but the code I'm using- I can hear those too. Don't know what I'm doing wrong. Here's what I used:
amp=10
fs=20500 % sampling frequency
duration=10
freq=30000
values=0:1/fs:duration;
a=amp*sin(2*pi* freq*values)
sound(a)
The duration has to be 1 second which is not the case either.
Any help is appreciated.
Thanks.

Answers (1)

Kirby Fears
Kirby Fears on 16 Sep 2015
Hi Varun,
To fix the duration problem, include the sampling frequency "fs" as the second argument to sound(). I can still hear some of the very high frequency range, though that might be due to imperfect hardware.
amp=10;
fs=20500; % sampling frequency
duration=1;
freq=30000;
values=0:1/fs:duration;
a=amp*sin(2*pi*freq*values);
sound(a,fs)
  3 Comments
Kirby Fears
Kirby Fears on 16 Sep 2015
Edited: Kirby Fears on 16 Sep 2015
Okay, mystery solved.
Your sampling frequency must be above the frequency you put into the signal. Previously any frequency above the sampling frequency was impossible to represent because the oscillations are faster than the sampling rate, creating a deeper (audible) sound. 20kHz is inaudible (for me at least) if you have a sampling rate of 80kHz, for example.
amp=1;
fs=80000; % sampling frequency
duration=1;
freq=20000;
values=0:1/fs:duration;
a=amp*sin(2*pi*freq*values);
sound(a,fs)
Star Strider
Star Strider on 16 Sep 2015
Most computer sound cards have an upper limit for the sampling frequency of 44.1 kHz, and the upper frequency reproduction limit for that is about 21 kHz. You can query the sound card information with the audiodevinfo function.
Higher frequencies generally require special driver circuitry and transducers.

Sign in to comment.

Categories

Find more on Audio I/O and Waveform Generation 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!