0

I want to create composite audio test signals in a numpy array. I need the values to be integers rounded to the nearest whole number. Secondly I want to add random noise or dither.

You can see below the code I use currently. This should comprise 24-bit integers (if I have got this right) even though it is stored in 32-bit integer words (to match pcm audio samples which are commonly stored this way).

In this example the first sine wave tone would be 1000.2Hz at half peak value (-6dBfs). There are two more sine wave components here though in practice it could contain any number them. I now want to add some dither. This would be calculated as a random value for each ordinate in the array. It would add a small randomly generated value of something like between + and - 0.5 peak to each array element (each audio sample).

x = np.linspace(0, 48000, 48000, False, dtype = "int32")

pcm_array:np.dtype(np.int32) = 
    (1073741824 * np.sin(((x + 0) * 1000.2 / 48000) * 2 * np.pi)) + 
    (10737418.24 * np.sin(((x + 0) * 1049.7 / 48000) * 2 * np.pi)) + 
    (1.073741824 * np.sin(((x + 0) * 1200.5 / 48000) * 2 * np.pi))

My code seems to work fine but does not include the random 'dither' component. Can someone let me know how to do that - and also reassure me that I will end up with 24-bit resolution integers.

Richard
  • 144
  • 1
  • 8
  • What's wrong with using `random.randrange()`? – zglin Oct 15 '19 at 16:24
  • Thanks zhqiat. My question would be how does one integrate that function as an additional component in this array generator? – Richard Oct 15 '19 at 16:30
  • I should have mentioned, I also need a way of setting the lowest 8 bits to all zeros or some such to get the resolution down from 32-bit to 24-bit. – Richard Oct 15 '19 at 16:36
  • Been a while since I did audio signal processing, but where you are doing the `x+0`, couldn't you replace the `+0` with a `+random.randrange(-1,1)/2` to simulate a 0.5 or -0.5 peak ? – zglin Oct 15 '19 at 16:40
  • The +0 components here are there to change the phase of the sine waves. Putting a random value here adds jitter (randomness in time - in the x axis). A worthwhile thing to do for some tests but not the random level component (randomness in the y axis) I need at the moment. – Richard Oct 15 '19 at 16:48

0 Answers0