MATLAB: How to generate a pure tone of 20 dB

20 dbchange dbchange decibelsdecibelshearingMATLABpure tonesine wave

I'm trying to generate a pure tone of 20 dB in matlab. I have the following code:
sr = 44100;
f = 100;
d = 1;
t = linspace(0,d,sr*d);
tone = sin(2*pi*f*t);

Best Answer

You cannot generate a tone of any particular loudness inside MATLAB. Inside MATLAB, you can generate digital values in the range +/- 1 that get converted to binary and sent to hardware that does something with the binary values. MATLAB cannot control what the hardware does with the binary values.
To phrase this another way: you can output the values to a sound card, or to a headphone jack, or to a Digital to Analog convertor, which will output small voltages (often not more than 1.2 V or so). Something is then responsible for amplifying the voltages through a speaker. But MATLAB cannot control the amount of amplification. If you lean over and turn down the volume knob, MATLAB would not even know.
In order to output a tone of a particular loudness, you need a calibrated system. And one of the things it needs to be calibrated for is how the response changes with frequency: 20 dB output at (say) 8 kHz might take a particular voltage, but 20 dB at (say) 8 Hz might take a lot more output because the system might not have a sub-woofer so outputting low frequencies might be very inefficient. 20 dB at 20 kHz might be enough to shake apart a system. -- so the calibration has to take into account the physical speakers, not just the electronics. Based upon the calibration information you can then figure out what relative amplitude you need to output -- but the relative values will still be in the range -1 to +1.