# Sine wave amplitude modulation - Analysis using Audacity

I am trying amplitude modulation in C++ on a sine wave which is a 1000 Hz sine wave sampled at 48000 Hz. The original sine wave is like this (In Audacity) -

When I basically double the amplitude for each sample, it works except for the flipping up of the extremities -

When I multiple each sample with 4, this is what I get -

Why is this the case, when the equations that I am using in the algorithm don't have anything of this sort? What if I want to remove this 'error'?

P. S. When I import the .raw data, I am using Signed 32-bit PCM encoding, Big-endian Byte order and single channel, with zero offset and 48kHz sample rate.

• this is really about fixed-point arithmetic or integer arithmetic. do you know how simple signed binary numbers are represented? it's about that. – robert bristow-johnson Nov 13 '19 at 0:49

Your samples have a finite bitwidth. For example, assume your samples have 16 bit width; if you increase their amplitude beyond what these 16 bit can represent, you end up with an overflow.

In case your audio samples being signed integers, that overflow typically first manifests as a sign inversion – exactly what you're seeing here. (That's because only the lower 15 bits in your 16 bit signed integer are actually the number – the uppest bit is the sign; you shift a 1 there, the result becomes a negative number in 2-complement represenation.)

This isn't a limitation of audio processing only – in fact, integer limits is something basically (nearly) every programmer needs to be (slightly) aware of. Your number variables can't hold arbitrarily large values!

• Thanks for the answer. I used standard float encoding, and that particular issue is gone. I have two follow up questions - 1. What software would you recommend for this kind of analysis applications (other than Audacity)? 2. Is there a way to see values outside the -1 to 1 range in Audacity? - tried finding an answer online, couldn't. – bimbi Nov 12 '19 at 21:59
• Scilab, Octave, Matlab, Sage, and Python with numpy are all good numerical analysis software packages. The first three are far more mature than numpy/Python or Sage, but I suspect Python of having longer legs -- or at least providing a nice in-between point between C/C++/Fortran and the other three. – TimWescott Nov 13 '19 at 1:15

Audacity is trying the "help" you by keeping your samples within the displayed range of -1.0 to +1.0.

Instead of clipping, Audacity is wrapping the out-of-range values completely around to a value of the opposite sign, which is typical of 2's complement arithmetic without overflow checks turned on. (perhaps scaled from the displayed floating point range of -1.0 to 1.0 to another signed integer range).

Remove the error by reducing the range of your results to legal values (displayed as no more the +1.0, no less than -1.0). This is required to feed legal sample values to your systems audio driver, audio DACs, and audio amplifier circuits, and etc.