setsnr

Capsim Block Documentation

Short Description

Set the Signal to Noise Raio (SNR) by calculating power over window of complex input samples and adding noise.

Top
Input Connections
Port Type Name
0 float inp
Top
Output Connections
Port Type Name
0 float out
Top
Parameters
Num Description Type Name Default Value
0 SNR, dB float snr 20
1 Preamble Size int preambleSize 100
2 Window Size int windowSize 50
3 Silence Threshold float silenceThreshold 0.000001
4 Seed for random number generator int seed 333
5 Verbose int verbose 0
Top
States
Num Type Name Initial Value Description
0 char rand_state[256]
1 double max
2 float dev
3 float variance
4 float* buffer_P
5 float* wbuffer_P
6 int windowCount 0
7 int preambleCount 0
8 int theState STATE_SILENCE
9 int dumpPreamble 1
10 int dumpWindow 0
11 int windowFull 0
12 float snrRatio
13 int totalCount 0
Top

Declarations


 

	int i,j,ok;
	int numin;
	int count = 0;
	int trouble;
	double s,t,u,v,k,w,x,sqrt(),log();
	float y1,y2;
	// long random();
        float val;
      float sum;
      float x1;
      float noisePower;



Top

Initialization Code



 

	srandom(seed);
	max = m;
	if (windowSize > preambleSize) {
		fprintf(stderr,"setsnr.s: windowSize cant be larger than preamble\n");
		return(4);
	}
      buffer_P=(float*)calloc(preambleSize, sizeof(float));
	if (!buffer_P) {
		fprintf(stderr,"setsnr.s: could not allocate space\n");
		return(3);
	}
      wbuffer_P=(float*)calloc(windowSize, sizeof(float));
	if (!wbuffer_P) {
		fprintf(stderr,"setsnr.s: could not allocate space\n");
		return(3);
	}
      preambleCount= windowSize;
      x1  = (snr)/ 10.0;
      snrRatio = pow(10.0,x1);




Top

Main Code



 

    if ((numin = AVAIL(0)) > 0) {
      while(count < numin) {
         IT_IN(0);
         totalCount++;
         val=inp(0);
         if(val==0.0  && theState== STATE_SILENCE) {
		       if(IT_OUT(0)) {
			              fprintf(stderr,"setsnr.s buffer 0 overflow\n");
			               return(99);
		       }
                           
                   out(0)=val;


         }else {
           wbuffer_P[windowCount]=val;
           windowCount++;
           if(theState == STATE_PREAMBLE ) {
                 
                 // store samples into preamble buffer
                 buffer_P[preambleCount]=val;
                 
                 preambleCount++;
                 if(preambleCount == preambleSize) {
                    if(verbose) fprintf(stderr,"STATE= STATE_SYMBOLS count=%d\n",totalCount);

                    theState= STATE_SYMBOLS;
                    preambleCount=0;

                 }
                 windowCount=0;

           }
           if(windowCount== windowSize) {
               windowFull=1;
               
               sum=0;
               for(i=0; isilenceThreshold && theState==STATE_SILENCE) {
                    if(verbose)fprintf(stderr,"STATE= STATE_PREAMBLE count=%d sum=%f \n",totalCount,sum);
                    theState=STATE_PREAMBLE;
                    for(i=0; i 100) {
			fprintf(stderr,"setsnr.s: problem with random number generator\n");
			return(2);
		}
		/* get two random numbers in the interval (-1,1) */
		s = random();
		u = -1.0 + 2.0*(s/max);
		t = random();
		v = -1.0 + 2.0*(t/max);
		w = u*u + v*v;
		/* is point (u,v) in the unit circle? */
	} while (w >= 1.0 || w == 0.0);

	x = sqrt((-2.0 * log(w))/w);
	/* find two independent values of y	*/
	y1 = dev * u * x;
	y2 = dev * v * x;
	
/****************** End of Gauss Code ****************************/

                      
		             if(IT_OUT(0)) {
			           fprintf(stderr,"setsnr.s buffer 0 overflow\n");
			           return(99);
		             }
                         val=buffer_P[preambleCount];
                         val += y1;
                         out(0)=val;
                         preambleCount++;
                     }
                  } 
                  if(dumpWindow && windowFull) {
                     // output window buffer
                     dumpWindow=0;
                     windowFull=0;
                     if(verbose) fprintf(stderr,"DUMPING WINDOW\n");
                     for(i=0; i< windowSize; i++) {
		             if(IT_OUT(0)) {
			           fprintf(stderr,"setsnr.s buffer 0 overflow\n");
			           return(99);
		             }

/****************************************************************/
/* 		gauss						*/
/* code written by Prayson Pate					*/
/* This code generates two random variables that are normally 	*/
/* distributed with mean 0 and variance 1 i.e N(0,1).	 	*/
/* The polar method is used to generate normally distributed    */
/* samples from a sequence that is uniform on (-1,1).  The      */
/* resulting distribution is described exactly by N(0,1).       */
/* This method is based	on the inverse distribution function.   */
/****************************************************************/
	trouble = 0;
	do {
		if(++trouble > 100) {
			fprintf(stderr,"setsnr.s: problem with random number generator\n");
			return(2);
		}
		/* get two random numbers in the interval (-1,1) */
		s = random();
		u = -1.0 + 2.0*(s/max);
		t = random();
		v = -1.0 + 2.0*(t/max);
		w = u*u + v*v;
		/* is point (u,v) in the unit circle? */
	} while (w >= 1.0 || w == 0.0);

	x = sqrt((-2.0 * log(w))/w);
	/* find two independent values of y	*/
	y1 = dev * u * x;
	y2 = dev * v * x;
	
/****************** End of Gauss Code ****************************/

                         val=wbuffer_P[i];
                         val += y1;
                         out(0)=val;


                    } // end for loop

                  } // end else
                  break;
            }

            }


		++count;

	}
      
	}

	return(0);




Top

Wrapup Code



 





Top

License



/*  Capsim (r) Text Mode Kernel (TMK) Star Library (Blocks)
    Copyright (C) 1989-2017  Silicon DSP Corporation

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    http://www.silicondsp.com
    Silicon DSP  Corporation
    Las Vegas, Nevada
*/


Top

Description



 
Author: Sasan Ardalan
Date: February 2003