0
votes

This may look like an easy question for some of you, but I'm not very familiar with signal processing, and I'd rather be sure about this.

So I've found some easy code to generate a pure tone in MATLAB:

Fs = 44100;
duration = 5.0;

numberOfSamples = Fs * duration;
samples = (1:numberOfSamples) / Fs;
s = sin(2 * pi * freq * samples);

sound(s, Fs);

I'd like to be able to adjust the volume in terms of dB here. More precisely, I want to introduce a certain parameter, say dbOffset, which, after calibration, determines how loud the tone is.

For example: if I calibrate so that dbOffset = 0 results in a tone of 50 dB, I want dbOffset = 10 to result in 60 dB, and so on.

Is this possible? Could anyone please help me with this?

Thanks a lot.

2

2 Answers

4
votes

The intensity of the sound (i.e. how loud it is) is determined by the amplitude of the sound wave. To increase or decrease the amplitude of a sine, you have to multiply it by a scale factor. Generally speaking:

 A*sin(2*pi*f)

yields a sine wave with a peak value (and therefore amplitude) of A, and frequency of f Hz. That, of course, in the continuous world.

That said, to control the sound intensity you have to multiply it by some constant.

Then, you have to look how the sound function works. From the official documentation:

The sound function assumes that y contains floating-point numbers between -1 and 1, and clips values outside that range.

That loosely translates to: if the amplitude y is equal to one, the soundcard will emit the loudest sound it can. Oops, there you face a problem: the actual sound intensity is mainly dependent on the sound card and the loudspeakers connected to the PC... So it's not that easy to make a generic function that spits out some sound in decibels...

If you want just to play with the concept, and get used to it, try:

Fs = 44100;
duration = 2.0;

numberOfSamples = Fs * duration;
samples = (1:numberOfSamples) / Fs;
s = sin(2 * pi * freq * samples);
s2 = 0.5*sin(2*pi*freq*samples);

sound(s, Fs);
sound(s2,Fs);

This will create a sound with a peak value that's half the peak value of the first one played. That does not mean that it has half the intensity, or, using better terms, the power. The power of a sinusoid is not linearly related to its amplitude, but that's a completely different story.

Bottom line is: to do what you want, you'll have to know the whole system involved in reproducing the sound, you'll have to know some physics to calculate the power, and then, just then, you'll be able to write some code that, multiplying the sinusoid by some calculated constant, plays a sound wave with a specified intensity :)

3
votes

Probably all that you need to know is that 20 dB is equal to a factor of 10, i.e.

dB = 20 * log10(V / V0)

So to increase the amplitude by 20 dB your peak value needs to go up by a factor of 10, and conversely -20 dB is a reduction in amplitude by a factor of 10.