0
$\begingroup$

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) -

48 kHz sine wave at 1000 Hz

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

enter image description here

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

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ this is really about fixed-point arithmetic or integer arithmetic. do you know how simple signed binary numbers are represented? it's about that. $\endgroup$ Commented Nov 13, 2019 at 0:49

2 Answers 2

1
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – bimbi
    Commented Nov 12, 2019 at 21:59
  • $\begingroup$ 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. $\endgroup$
    – TimWescott
    Commented Nov 13, 2019 at 1:15
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.