DSPRelated.com
Forums

Variance Calculation Using Blackfin

Started by Gnanavelu January 30, 2010
Hi
I am determining the Variance in Blackfin DSP using assembly language.
I compared with the c code under following cases

1. 8 samples
2. 16 samples
3. 32 samples
4. 1024 samples.

I got correct result under case 1 and case 2. But i am not getting correct result under case 3 and 4. C and Assembly codes are below. Also shared at
http://www.4shared.com/file/210861924/d79c05b4/VarianceCalc.html
Can somebody tell me where is the wrong.

#include
#include
#include

#define MAX_SIZE 32//1024

float InputData[MAX_SIZE] ={
0.25005,0.59361,0.16858,0.66254,
0.73142,0.14195,-0.42078,-0.21316,
-0.28167,0.68526,0.76273,0.35498,
0.5977,0.17204,0.24829,0.052611
-0.1665,-0.79421,0.062438,0.094364,
0.43401,-0.011778,0.17192,0.98137,
0.54351,0.91544,-0.97341,0.12246,
0.0055164,-0.014214,0.97314,0.36023
/* 0.44436,-0.43146,0.20522,0.47057,
0.6912,0.44631,0.41747,0.23028,
-0.86039,0.94002,-0.31636,0.97059,
-0.88821,0.82206,0.64462,0.835,
0.047385,0.59136,0.99241,0.25644,
0.10618,0.24282,0.32284,0.55811,
-0.29633,0.70868,-0.43582,-0.84618,
0.24908,-0.82105,-0.82567,0.25509,
0.73364,0.66384,0.78594,0.7538,
0.99733,0.7704,0.88123,-0.35055,
0.80984,-0.97032,0.23707,

*/
// #include "RandomSig.dat"
};

short InputDataFr16[MAX_SIZE];
short VarianceVal;

void main(){
short i;
float Sum=0.0;
float MeanValue;
float SquareDiff;
float M;
float VarianceFloat,VarianceSum=0.0;
for (i=0;i InputDataFr16[i] = float_to_fr16(InputData[i]);
}

for(i=0;i Sum += InputData[i];
}

M = (float)MAX_SIZE;
MeanValue = Sum/M;
printf( " Average Using C is %f\n",MeanValue);

for(i=0;i SquareDiff = pow((InputData[i]-MeanValue),2.0);
// printf("%f\n",SquareDiff);
VarianceSum += SquareDiff;
}
VarianceSum = VarianceSum/M;
printf("Variance is %f\n", VarianceSum);


_VarianceCalculate(&InputDataFr16[0],&VarianceVal,MAX_SIZE);
VarianceFloat = fr16_to_float(VarianceVal);
printf("Variance Value Using Assembly = %f\n", VarianceFloat);

}

.section/code program;
.global __VarianceCalculate;

__VarianceCalculate:
I0 = R0; // Signal
I1 = R1; // Variance Address
LC0 = R2; // Buffer Length
LC1 = R2;
R3 = 0;
R4 = 0;
LSETUP(_AverageStart,_AverageEnd) LC0;
_AverageStart:
R3.H = W[ I0 ++ ];
R3 = R3>>>5;
_AverageEnd:
R4 = R4 + R3; // Mean Value at R4 register
I0 = R0;
R3 = 0;
A0 = 0;

LSETUP(_VarianceStart,_VarianceEnd) LC1;
_VarianceStart:
R3.H = W[ I0 ++ ];
R2 = R3 - R4;
_VarianceEnd:
A0 +=R2.H*R2.H;
A0 = A0>>>5;
R0 = A0;
W[ I1 ] = R0.H;
RTS;
NOP;
__VarianceCalculate.end:

Regards
Gnanavel

Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/
Dear Velu san,
Blackfin for simple people and simple applications ( !!!!! dont take it as it is... but hint is the following).

Blackfin will be using ACC to store sum value that comes out of each iteration of for loop. I think ACC itself getting overflow mode when you go for 32 or 1024 samples based for loop. If we assume this is true then we can find N number of solutions for the same.

most primitive....
just make many ( needful number) 16 samples loop to solve 32 or 1024. In this, mem will be used as a ACC at the end for loop ( 16 times) and add them or divide them and add them. I mean you can divide each loop by 16 to get value and add result... this will help. If you want c code i can write but i want you to write and enjoy the joy working with primitive computing machines.
Advanced hint.
Select fractional format such that ACC does not overflow ( this will be data dependent and you need to know more details about your data array). Without details about data array it is not east to decide format.

Have a nice week end.

Kind Regards
jk

----- Original Message -----
From: Gnanavelu
To: a...
Sent: Saturday, January 30, 2010 9:18 AM
Subject: [adsp] Variance Calculation Using Blackfin

Hi
I am determining the Variance in Blackfin DSP using assembly language.
I compared with the c code under following cases

1. 8 samples
2. 16 samples
3. 32 samples
4. 1024 samples.

I got correct result under case 1 and case 2. But i am not getting correct result under case 3 and 4. C and Assembly codes are below. Also shared at
http://www.4shared.com/file/210861924/d79c05b4/VarianceCalc.html
Can somebody tell me where is the wrong.

#include
#include
#include

#define MAX_SIZE 32//1024

float InputData[MAX_SIZE] ={
0.25005,0.59361,0.16858,0.66254,
0.73142,0.14195,-0.42078,-0.21316,
-0.28167,0.68526,0.76273,0.35498,
0.5977,0.17204,0.24829,0.052611
-0.1665,-0.79421,0.062438,0.094364,
0.43401,-0.011778,0.17192,0.98137,
0.54351,0.91544,-0.97341,0.12246,
0.0055164,-0.014214,0.97314,0.36023
/* 0.44436,-0.43146,0.20522,0.47057,
0.6912,0.44631,0.41747,0.23028,
-0.86039,0.94002,-0.31636,0.97059,
-0.88821,0.82206,0.64462,0.835,
0.047385,0.59136,0.99241,0.25644,
0.10618,0.24282,0.32284,0.55811,
-0.29633,0.70868,-0.43582,-0.84618,
0.24908,-0.82105,-0.82567,0.25509,
0.73364,0.66384,0.78594,0.7538,
0.99733,0.7704,0.88123,-0.35055,
0.80984,-0.97032,0.23707,

*/
// #include "RandomSig.dat"
};

short InputDataFr16[MAX_SIZE];
short VarianceVal;

void main(){
short i;
float Sum=0.0;
float MeanValue;
float SquareDiff;
float M;
float VarianceFloat,VarianceSum=0.0;
for (i=0;i InputDataFr16[i] = float_to_fr16(InputData[i]);
}

for(i=0;i Sum += InputData[i];
}

M = (float)MAX_SIZE;
MeanValue = Sum/M;
printf( " Average Using C is %f\n",MeanValue);

for(i=0;i SquareDiff = pow((InputData[i]-MeanValue),2.0);
// printf("%f\n",SquareDiff);
VarianceSum += SquareDiff;
}
VarianceSum = VarianceSum/M;
printf("Variance is %f\n", VarianceSum);

_VarianceCalculate(&InputDataFr16[0],&VarianceVal,MAX_SIZE);
VarianceFloat = fr16_to_float(VarianceVal);
printf("Variance Value Using Assembly = %f\n", VarianceFloat);

}

.section/code program;
.global __VarianceCalculate;

__VarianceCalculate:
I0 = R0; // Signal
I1 = R1; // Variance Address
LC0 = R2; // Buffer Length
LC1 = R2;
R3 = 0;
R4 = 0;
LSETUP(_AverageStart,_AverageEnd) LC0;
_AverageStart:
R3.H = W[ I0 ++ ];
R3 = R3>>>5;
_AverageEnd:
R4 = R4 + R3; // Mean Value at R4 register
I0 = R0;
R3 = 0;
A0 = 0;

LSETUP(_VarianceStart,_VarianceEnd) LC1;
_VarianceStart:
R3.H = W[ I0 ++ ];
R2 = R3 - R4;
_VarianceEnd:
A0 +=R2.H*R2.H;
A0 = A0>>>5;
R0 = A0;
W[ I1 ] = R0.H;
RTS;
NOP;
__VarianceCalculate.end:

Regards
Gnanavel

------
Your Mail works best with the New Yahoo Optimized IE8. Get it NOW!.
What jk said is correct - in your C you have defined things as "float",
but in assembler you are using ints. Look at the assembly from the
C compiler (-S switch or click on something that will generate it).

To get different results for both C and assembler, look into setting up
the "saturation mode". And for both integer and fractional modes.
You will get a lot of different answers - all of them correct :-)

Patience, persistence, truth,
Dr. mike

On Sat, 30 Jan 2010, Dr Jayakumar Singaram wrote:

> Dear Velu san,
> Blackfin for simple people and simple applications ( !!!!! dont take it as it is... but hint is the following).
>
> Blackfin will be using ACC to store sum value that comes out of each iteration of for loop. I think ACC itself getting overflow mode when you go for 32 or 1024 samples based for loop. If we assume this is true then we can find N number of solutions for the same.
>
> most primitive....
> just make many ( needful number) 16 samples loop to solve 32 or 1024. In this, mem will be used as a ACC at the end for loop ( 16 times) and add them or divide them and add them. I mean you can divide each loop by 16 to get value and add result... this will help. If you want c code i can write but i want you to write and enjoy the joy working with primitive computing machines.
> Advanced hint.
> Select fractional format such that ACC does not overflow ( this will be data dependent and you need to know more details about your data array). Without details about data array it is not east to decide format.
>
> Have a nice week end.
>
> Kind Regards
> jk
>
> ----- Original Message -----
> From: Gnanavelu
> To: a...
> Sent: Saturday, January 30, 2010 9:18 AM
> Subject: [adsp] Variance Calculation Using Blackfin
>
> Hi
> I am determining the Variance in Blackfin DSP using assembly language.
> I compared with the c code under following cases
>
> 1. 8 samples
> 2. 16 samples
> 3. 32 samples
> 4. 1024 samples.
>
> I got correct result under case 1 and case 2. But i am not getting correct result under case 3 and 4. C and Assembly codes are below. Also shared at
> http://www.4shared.com/file/210861924/d79c05b4/VarianceCalc.html
> Can somebody tell me where is the wrong.
>
> #include
> #include
> #include #define MAX_SIZE 32//1024
>
> float InputData[MAX_SIZE] ={
> 0.25005,0.59361,0.16858,0.66254,
> 0.73142,0.14195,-0.42078,-0.21316,
> -0.28167,0.68526,0.76273,0.35498,
> 0.5977,0.17204,0.24829,0.052611
> -0.1665,-0.79421,0.062438,0.094364,
> 0.43401,-0.011778,0.17192,0.98137,
> 0.54351,0.91544,-0.97341,0.12246,
> 0.0055164,-0.014214,0.97314,0.36023
> /* 0.44436,-0.43146,0.20522,0.47057,
> 0.6912,0.44631,0.41747,0.23028,
> -0.86039,0.94002,-0.31636,0.97059,
> -0.88821,0.82206,0.64462,0.835,
> 0.047385,0.59136,0.99241,0.25644,
> 0.10618,0.24282,0.32284,0.55811,
> -0.29633,0.70868,-0.43582,-0.84618,
> 0.24908,-0.82105,-0.82567,0.25509,
> 0.73364,0.66384,0.78594,0.7538,
> 0.99733,0.7704,0.88123,-0.35055,
> 0.80984,-0.97032,0.23707,
>
> */
> // #include "RandomSig.dat"
> };
>
> short InputDataFr16[MAX_SIZE];
> short VarianceVal;
>
> void main(){
> short i;
> float Sum=0.0;
> float MeanValue;
> float SquareDiff;
> float M;
> float VarianceFloat,VarianceSum=0.0;
> for (i=0;i > InputDataFr16[i] = float_to_fr16(InputData[i]);
> }
>
> for(i=0;i > Sum += InputData[i];
> }
>
> M = (float)MAX_SIZE;
> MeanValue = Sum/M;
> printf( " Average Using C is %f\n",MeanValue);
>
> for(i=0;i > SquareDiff = pow((InputData[i]-MeanValue),2.0);
> // printf("%f\n",SquareDiff);
> VarianceSum += SquareDiff;
> }
> VarianceSum = VarianceSum/M;
> printf("Variance is %f\n", VarianceSum);
> _VarianceCalculate(&InputDataFr16[0],&VarianceVal,MAX_SIZE);
> VarianceFloat = fr16_to_float(VarianceVal);
> printf("Variance Value Using Assembly = %f\n", VarianceFloat);
>
> }
>
> .section/code program;
> .global __VarianceCalculate;
>
> __VarianceCalculate:
> I0 = R0; // Signal
> I1 = R1; // Variance Address
> LC0 = R2; // Buffer Length
> LC1 = R2;
> R3 = 0;
> R4 = 0;
> LSETUP(_AverageStart,_AverageEnd) LC0;
> _AverageStart:
> R3.H = W[ I0 ++ ];
> R3 = R3>>>5;
> _AverageEnd:
> R4 = R4 + R3; // Mean Value at R4 register
> I0 = R0;
> R3 = 0;
> A0 = 0;
>
> LSETUP(_VarianceStart,_VarianceEnd) LC1;
> _VarianceStart:
> R3.H = W[ I0 ++ ];
> R2 = R3 - R4;
> _VarianceEnd:
> A0 +=R2.H*R2.H;
> A0 = A0>>>5;
> R0 = A0;
> W[ I1 ] = R0.H;
> RTS;
> NOP;
> __VarianceCalculate.end:
> Regards
> Gnanavel
>
> ------
> Your Mail works best with the New Yahoo Optimized IE8. Get it NOW!.
Dear Mike and JK,

Thanks for your suggestion. but your suggestions are wrong. The actual problem is number format. In my assembly codeĀ  i used Q15 format.The Average value is 0.225326. When you find the difference from the mean. For an example , the sample value (-0.79421-0.225326) you will get -1.01951. Actually Q15 format range is from -0.9 to 0.9. So to avoid this i convert Q15 to Q14 format then solved the issue.

Regards
GJ
--- On Sun, 31/1/10, Mike Rosing wrote:

From: Mike Rosing
Subject: Re: [adsp] Variance Calculation Using Blackfin
To: "Dr Jayakumar Singaram"
Cc: a..., "Gnanavelu"
Date: Sunday, 31 January, 2010, 7:37 AM



What jk said is correct - in your C you have defined things as "float",

but in assembler you are using ints. Look at the assembly from the

C compiler (-S switch or click on something that will generate it).

To get different results for both C and assembler, look into setting up

the "saturation mode". And for both integer and fractional modes.

You will get a lot of different answers - all of them correct :-)

Patience, persistence, truth,

Dr. mike

On Sat, 30 Jan 2010, Dr Jayakumar Singaram wrote:

> Dear Velu san,

> Blackfin for simple people and simple applications ( !!!!! dont take it as it is... but hint is the following).

>

> Blackfin will be using ACC to store sum value that comes out of each iteration of for loop. I think ACC itself getting overflow mode when you go for 32 or 1024 samples based for loop. If we assume this is true then we can find N number of solutions for the same.

>

> most primitive... .

> just make many ( needful number) 16 samples loop to solve 32 or 1024. In this, mem will be used as a ACC at the end for loop ( 16 times) and add them or divide them and add them. I mean you can divide each loop by 16 to get value and add result... this will help. If you want c code i can write but i want you to write and enjoy the joy working with primitive computing machines.

>

>

> Advanced hint.

> Select fractional format such that ACC does not overflow ( this will be data dependent and you need to know more details about your data array). Without details about data array it is not east to decide format.

>

> Have a nice week end.

>

> Kind Regards

> jk

>

> ----- Original Message -----

> From: Gnanavelu

> To: adsp@yahoogroups. com

> Sent: Saturday, January 30, 2010 9:18 AM

> Subject: [adsp] Variance Calculation Using Blackfin

>

>

>

> Hi

> I am determining the Variance in Blackfin DSP using assembly language.

> I compared with the c code under following cases

>

> 1. 8 samples

> 2. 16 samples

> 3. 32 samples

> 4. 1024 samples.

>

> I got correct result under case 1 and case 2. But i am not getting correct result under case 3 and 4. C and Assembly codes are below. Also shared at

> http://www.4shared. com/file/ 210861924/ d79c05b4/ VarianceCalc. html

>

>

> Can somebody tell me where is the wrong.

>

> #include

> #include

> #include

>

> #define MAX_SIZE 32//1024

>

> float InputData[MAX_ SIZE] ={

> 0.25005,0.59361, 0.16858,0. 66254,

> 0.73142,0.14195, -0.42078, -0.21316,

> -0.28167,0.68526, 0.76273,0. 35498,

> 0.5977,0.17204, 0.24829,0. 052611

> -0.1665,-0.79421, 0.062438, 0.094364,

> 0.43401,-0.011778, 0.17192,0. 98137,

> 0.54351,0.91544, -0.97341, 0.12246,

> 0.0055164,-0. 014214,0. 97314,0.36023

> /* 0.44436,-0.43146, 0.20522,0. 47057,

> 0.6912,0.44631, 0.41747,0. 23028,

> -0.86039,0.94002, -0.31636, 0.97059,

> -0.88821,0.82206, 0.64462,0. 835,

> 0.047385,0.59136, 0.99241,0. 25644,

> 0.10618,0.24282, 0.32284,0. 55811,

> -0.29633,0.70868, -0.43582, -0.84618,

> 0.24908,-0.82105, -0.82567, 0.25509,

> 0.73364,0.66384, 0.78594,0. 7538,

> 0.99733,0.7704, 0.88123,- 0.35055,

> 0.80984,-0.97032, 0.23707,

>

> */

> // #include "RandomSig.dat"

> };

>

> short InputDataFr16[ MAX_SIZE] ;

> short VarianceVal;

>

> void main(){

> short i;

> float Sum=0.0;

> float MeanValue;

> float SquareDiff;

> float M;

> float VarianceFloat, VarianceSum= 0.0;

> for (i=0;i
> InputDataFr16[ i] = float_to_fr16( InputData[ i]);

> }

>

> for(i=0;i
> Sum += InputData[i] ;

> }

>

> M = (float)MAX_SIZE;

> MeanValue = Sum/M;

> printf( " Average Using C is %f\n",MeanValue) ;

>

> for(i=0;i
> SquareDiff = pow((InputData[ i]-MeanValue) ,2.0);

> // printf("%f\n" ,SquareDiff) ;

> VarianceSum += SquareDiff;

> }

> VarianceSum = VarianceSum/ M;

> printf("Variance is %f\n", VarianceSum) ;

>

>

> _VarianceCalculate( &InputDataFr16[ 0],&VarianceVal, MAX_SIZE) ;

> VarianceFloat = fr16_to_float( VarianceVal) ;

> printf("Variance Value Using Assembly = %f\n", VarianceFloat) ;

>

> }

>

>

>

>

>

> .section/code program;

> .global __VarianceCalculate ;

>

> __VarianceCalculate :

> I0 = R0; // Signal

> I1 = R1; // Variance Address

> LC0 = R2; // Buffer Length

> LC1 = R2;

> R3 = 0;

> R4 = 0;

> LSETUP(_AverageStar t,_AverageEnd) LC0;

> _AverageStart:

> R3.H = W[ I0 ++ ];

> R3 = R3>>>5;

> _AverageEnd:

> R4 = R4 + R3; // Mean Value at R4 register

> I0 = R0;

> R3 = 0;

> A0 = 0;

>

> LSETUP(_VarianceSta rt,_VarianceEnd) LC1;

> _VarianceStart:

> R3.H = W[ I0 ++ ];

> R2 = R3 - R4;

> _VarianceEnd:

> A0 +=R2.H*R2.H;

> A0 = A0>>>5;

> R0 = A0;

> W[ I1 ] = R0.H;

> RTS;

> NOP;

> __VarianceCalculate .end:

>

>

>

>

> Regards

> Gnanavel

>