Sunday, 15 June 2014

math - Sine wave alternates distortion in Java -



math - Sine wave alternates distortion in Java -

i'm trying generate sine wave , add together byte array. searched , found. however, distorted waveform attachment.

please give me sentiment why happens. thanks.

my code here

private byte[] getdata(int freq) { // taking pitch info double pha = math.pi/2; // defining phase final int length = 44100 * 10; // defining length of sine wave, byte array final byte[] arr = new byte[length]; for(int = 0; < arr.length; i++) { double angle = (2.0 * math.pi * i*freq+pha) / (44100); arr[i] = (byte) (math.cos(angle) *127* 0.3); // 0.3 amplitude scale } homecoming arr; }

distort waveform illustration pic

the code looks fine. suspect it's visualiser interpreting two's complement signed values unsigned (-1 becoming 255, -2 becoming 254 , on).

i write wav file , plot sonicvisualiser

according wave pcm soundfile format:

8-bit samples stored unsigned bytes, ranging 0 255. 16-bit samples stored 2's-complement signed integers, ranging -32768 32767.

it looks either need shift sine wave 128 (so fits within 0-255 range), or move using 16-bit samples.

java math signals sine

No comments:

Post a Comment