1 Reply Latest reply on Dec 13, 2010 1:09 PM by gautam.das.g

    calculate the  efective value

    gligor.shijakovski

       Hi to everyone! I would use some help about one assigment, and I'll be grateful if you could help me. The assigment is on two input signal, I need to calculate the  efective value and I need to let the signal who has bigger effective value on the gate. My solution on this problem is the following:

         

       

         
          I squere and add every sample and I do that for some particular number of sample, in my case that number is 65535. After that the sum which is equal to the sum of the sgures I devide it with 65535, and then I find a root on that rusult. In that way I get the effective value. After that i compere the two effective value and I let to the gate the bigger one.   
         
          my code is:   
         
              
         
          
            #include <device.h>    
          
            #include <math.h>    
          
            double  result = 0;    
          
            double  konecen = 0;    
          
            double  konecen1 = 0;    
          
            int32 suma1=0;    
          
            int32 suma2=0;    
          
            double  result1 = 0;    
          
            int32 br1=0;    
          
            int32 br2=0;    
          
            void main()    
          
            {    
          
            ADC_DelSig_1_Start(); /* Initialize ADC */    
          
                for(;;)    
          
                {    
          
                    AMux_1_Start( ); /* Reset all channels */    
          
            AMux_1_Select(0); /* Connect channel 1 */    
          
            ADC_DelSig_1_StartConvert();    
          
            ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT);    
          
            ADC_DelSig_1_StopConvert();    
          
            suma1+=ADC_DelSig_1_GetResult16() * ADC_DelSig_1_GetResult16();    
          
            br1+=1;      
          
                    if (br1==65535)    
          
                 
          
            {    
          
            result = suma1/65535 ;    
          
            konecen = sqrt(result);    
          
                
          
                    }    
          
            AMux_1_Select(1); /* Connect channel 2 */    
          
            ADC_DelSig_1_StartConvert();    
          
            ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT);    
          
            suma2+=ADC_DelSig_1_GetResult16() * ADC_DelSig_1_GetResult16();    
          
            br2+=1;    
          
            if (br2==65535)    
          
                  
          
            {    
          
            result1 = suma2/65535 ;    
          
            konecen1 = sqrt(result1);    
          
                
          
                    }    
          
              if( br1==65535 && br2==65535)    
          
              {    
          
                br1=0;    
          
            br2=0;    
          
            suma1=0;    
          
            suma2=0;    
          
                   if(konecen > konecen1)    
          
                {     
          
            AMux_2_Start( );    
          
            AMux_2_Select(0);    
          
            }    
          
                    else    
          
                    {    
          
                AMux_2_Select(1);    
          
            }     
          
            break;    
          
            }    
          
                
          
              }     
          
            }    
         
         
              
         
          
           Do you think this will work? Has anyone have a sugestion how I can advance the code? Then how can I make a conversion from a binary record on finaly and finaly1 into decimal becouse I will need to show it on the screen so I need the easiest way to do that? That should look like this:            
          
            Input signal1 effective value=number        
          
            Input signal2 effective value=number       
          
            output=signal1 or signal 2 it depends how is bigger    
          
            Is it better if I calculate the sum of the root with the interapt routine form adc?    
          
            Do you have any other ideas about how I can solve this problem? Thanks to everybody !!    
         
        • 1. Re: calculate the  efective value
          gautam.das.g

          Hi GiGo,

             

           

             

          I have few suggestions regarding this code.

             

          1) suma1 is used as an accumulator for storing the square of two 16 bit ADC readings. Assuming that the ADC output occupies entire 16 bits, you'll ideally be getting a 32 bit result. You have declared suma1 as int32 which means its a signed int covering both negative and positive range.

             

          The square of a number can never be negative, so I presume that its better that you use uint32 instead of int32. This will start counting from 0.

             

          2) Another cause of worry is that you can't successfully accumulate in this way..!!! Thats because, just with a single reading you'll be generating a 32-bit value. You can no longer add any other successive ADC's squared reading to this. It just keeps rolling over again and again. 

             

          Solution: I think that you must be more concerned about the upper 16 bits which is more significant. Hence you can throw the lower 16 bits from every squared value by right shifting 16 times ( >> 16). Since you are adding 65536 similar such samples, it'll occupy 16 more bits. So, the tally come up to 16 + 16 bits = 32 bits. If you have declared it as uint32 the overflow is avoided.

             

           

             

          Please let us know if this works.

             

           

             

          Cheers,

             

          dasg