# A question on difference between an computationally created sinusoid signal and the recorded signal (when played out through a speaker)?

I have created the following signal(in Matlab), a 5000 Hz sinusoid multiplied with a bump (just to limit it in time) so that it forms a short beep. Now if i observe the spectrum of this computationally created signal (in matlab) there is an impulse like structure at 5000Hz and nothing else.

Now i perform this experiment. I play this signal in matlab through a sound card into a speaker and record the sound played on the speaker through a microphone. The surprising thing is that if i observe the spectrum of this recorded signal, i find a impulse like structure at 5000Hz as expected but there is a another impulse like structure of much lower amplitude at 10000Hz and an even much smaller one at 15000Hz. I wonder how it can be explained.

When I repeated this experiment with 2000Hz I found same observation at 4000Hz and 8000Hz.

What you are seeing are harmonics. (Multiples of the fundamental frequency). These indicate that your system has distortion. Harmonic distortion, loosely speaking, is a change in the shape of the input due to a nonlinear behavior in the signal chain. It adds frequencies which are not present in the original signal.

Inside Matlab, your generated waveform is pure data, and so it has no distortion (except for the tiny errors in the floating-point representation). The distortion happens in your analog chain: in your audio interface output, amplifier, speaker, microphone, microphone preamp, audio interface input.

See if you can fine tune the gains of these components to reduce these harmonics. The gain structure over multiple parts in the signal path should be balanced. Loosely speaking, you don't want some part not having enough gain and cranking up another part to compensate.

Also another thing to see is whether the distortion goes away if you make the signal much smaller. Some forms of distortion stay loud as the signal is decreased (e.g. cross-over distortion in a class-AB amplifier.). Some forms of distortion diminish or go away. (e.g. clipping.)

You need, surprise surprise, quality hardware if you want to recapture a pure signal without distortion. How much did you spend on your audio interfaces, speakers, mic, etc.

A professional quality audio interface can set you back \$1000 and up. Good set of studio monitor speakers, likewise. Good microphone: hundred bucks minimum.

Not much to add to Kaz's answer, except for maybe a note on simulating distortion inside Matlab. You can experiment more things about distortion, and faster, if you simulate it within Matlab. You can do this until you grasp some intuition of it, and then once in a while do real-life experiments (which take longer).

For instance, if $y$ is your undistorted signal (the one with the pure sinusoid), whose amplitude goes from -1 to +1, you can easily create a distorted version $y_d$ of it, doing this:

% This is jus one possible way to distort a signal.
gamma=0.8;  % gamma=1 means no distortion.
yd=sign(y).*(abs(y).^gamma);

And you would see something like this, in Matlab:

(Spectra in dB, and using a Blackman Harris window.)

For this type of distortion, the only harmonics created are odd (odd multiples of 5 kHz). Other types create odd and even harmonics.

If you're getting harmonics, that suggests the presence of distortion. One way to test for distortion by ear is to generate a signal containing two sine waves, one at a fixed amplitude and frequency (e.g. 2000Hz) and the other one sweeping from e.g. 300Hz to 800Hz. In the absence of distortion, only be one sweeping frequency should be audible, but the presence of distortion will usually create other frequencies, some of which will sweep in the opposite direction. A small amount of distortion may not cause noticeable aliasing, but the presence of aliasing is a dead giveaway to distortion.