Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2016 21:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
private static byte[] mixSignals(byte[] signal, byte[] noise) {
ByteBuffer bb = ByteBuffer.wrap(signal);
ShortBuffer sb = bb.asShortBuffer();
short[] signalArray = new short[sb.capacity()];
ByteBuffer bb2 = ByteBuffer.wrap(noise);
ShortBuffer sb2 = bb2.asShortBuffer();
short[] noiseArray = new short[sb2.capacity()];
float max = 0;
for (int i=0;i<signalArray.length;i++){
if (noiseArray.length>i){
if (Math.abs(signalArray[i] + noiseArray[i])>max){
max = Math.abs(signalArray[i]+noiseArray[i]);
int a,b,c;
for (int i=0;i<signalArray.length;i++){
a = signalArray[i];
b = noiseArray[i];
c = Math.round(Short.MAX_VALUE *
(a + b) / max);
if (c>Short.MAX_VALUE){
c = Short.MAX_VALUE;
else if(c<Short.MIN_VALUE){
c = Short.MIN_VALUE;
signalArray[i] = (short) c;
byte[] result = new byte[signalArray.length*2];
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment