# Noise when dividing or multiplying signal in C

I'm trying to create a echo effect and have managed to add multiple delays but when i try to divide or multiply these numbers the signal is completely distorted and adds a lot of noise to the system.

This is my some of my program written in C.

            unsigned char txBuf0[AUDIO_BUF_SIZE];
unsigned char rxBuf0[AUDIO_BUF_SIZE];

int i, n, x;
unsigned short Stor0[2000], Stor1[2000],Stor2[2000], Stor3[2000], z, ze;

for(i=4; i < AUDIO_BUF_SIZE; i+= 4)
{
x= i/4;
if(lastFullRxBuf == 0){
Stor3[x] = Stor2[x]/2;
Stor2[x] = Stor1[x]/2;
Stor1[x]= Stor0[x]/2;
Stor0[x] = 0;
Stor0[x] =((rxBuf0[i-4]) | (rxBuf0[i-3])<<8);

z = (Stor0[x]) + (Stor1[x])+ (Stor2[x]) +(Stor3[x]);

rxBuf0[i-4]= z;
rxBuf0[i-3]= z>>8;

txBuf0[i-4]=rxBuf0[i-4];
txBuf0[i-3]=rxBuf0[i-3];
}

• The first thing to check would be is z is not overflowing. Also, shifting an 8-bit char eight times seems suspicious. It'd be a good idea to comment your code and explain what your algorithms is. – MBaz Apr 27 '18 at 14:03
• what about txBuf0[i-1], txBuf0[i-2], rxBuf0[i-1], rxBuf0[i-2]?? what's in them samples? – robert bristow-johnson Apr 28 '18 at 1:26
• Thanks for the help these have all cleared up a few of the problems. Ya I’ve missed a lot out this is just some snipet of my program it’s a bit long so I can see the confusion sorry about that. – GeMaz Apr 29 '18 at 17:07

I don't think this code is doing what you intend it to do. However, you can do it more efficiently with the following code.

        unsigned char txBuf0[AUDIO_BUF_SIZE];
unsigned char rxBuf0[AUDIO_BUF_SIZE];

int i, n, x;
unsigned short Stor0[2000], Stor1[2000],Stor2[2000], Stor3[2000], z, ze;

unsigned short* txIntBuf0 = (unsigned short*) txBuf0;
unsigned short* rxIntBuf0 = (unsigned short*) rxBuf0;

int limit = (AUDIO_BUF_SIZE >> 1) - 2;

x = 1

for( i = 0; i < limit; i+= 2 )
{
if( lastFullRxBuf == 0 )
{
Stor3[x] = Stor2[x] >> 1;
Stor2[x] = Stor1[x] >> 1;
Stor1[x] = Stor0[x] >> 1;
Stor0[x] = rxIntBuf0[i];

z = Stor0[x] + Stor1[x] + Stor2[x] + Stor3[x];

rxIntBuf0[i] = z;
txIntBuf0[i] = rxIntBuf0[i];

}
x++;

}



Here's just some of the issues I see:

• 16 bit audio data is usually signed

• Setting your rx buffer doens't make sense. Isn't this where you got the data from?

• The if statement inside the loop can be put outside the loop (unless code is missing)

• It is quite possible for z to overflow (most likely your problem)

Anyway, I hope this helps at bit.

Ced

In processing integer audio samples, simple non-scaled integer division can introduce quantization noise (rounding errors).

You might try doing your amplitude scaling using a higher resolution format (floats or scaled integers with more fractional bits) for your arithmetic, and then perform appropriate noise filtering (dithering or noise shaping of the remainder from the quantization process) if a shorter integer result is required.

Also watch out for integer overflow (e.g. make sure your number formats can hold the full range of expected arithmetic results).