Routine bas-niveau
fr - eng


noiseblk_c - routine de calcul générateur de bruit blanc gaussien

Module

Paramètres

Contenu du fichier


/* noiseblk_c subroutine
 * Gaussian Noise generator
 * with Box Muller Law method
 *
 * Copyright (C) 2007-2011 Alan Layec
 *
 * This file is part of modnumlib.
 *
 * modnumlib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * modnumlib 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with modnumlib; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* REVISION HISTORY :
 * $Log$
 */

#include <math.h>
#include <stdlib.h>  /*pour RAND_MAX*/

#include "modnum_lib.h"

/* noiseblk_c routine de calcul d'échantillons bruités par la méthode "Box Muller Law"
 *
 * Entrées :
 * n    : taille des vecteurs
 * typ  : type de sortie (0:cos/1:sin)
 * sig  : variance (scalaire)
 * mean : moyenne (scalaire)
 * Sorties :
 * y    : vecteur des sorties
 *
 * dépendances
 * math.h
 *
 */

void noiseblk_c(int *n,int *typ,double *sig,double *mean,double *y)
{
 /*déclaration des variables*/
 int i;
 double rand1, rand2;
 double rand_m;
 double ampl, phase;

 /*récupération de la valeur de RAND_MAX*/
 rand_m=RAND_MAX;

 for(i=0;i<(*n);i++)
 {
  /*calcul rand1*/
  rand1=rand()/rand_m;
  /*test rand1*/
  while((rand1<=0)||(rand1>=1)) rand1=rand()/rand_m;

  /*calcul rand2*/
  rand2=rand()/rand_m;
  /*test rand2*/
  while((rand2<=0)||(rand2>=1)) rand2=rand()/rand_m;

  /*Calcul amplitude et phase*/
  ampl=(*sig)*sqrt(2*-log(rand1));
  phase=2*M_PI*rand2;

  /*Calcul y*/
  if((*typ)==0) 
  y[i]=(*mean)+ampl*cos(phase);
  else if((*typ)==1)
  y[i]=(*mean)+ampl*sin(phase);
 }
 return;
}

/* noiseblkv_c routine de calcul d'échantillons bruités par la méthode "Box Muller Law"
 *
 * Entrées :
 * n    : taille des vecteurs
 * typ  : type de sortie (0:cos/1:sin)
 * sig  : vecteurs des variances
 * mean : vecteurs des moyennes
 * Sorties :
 * y    : vecteur des sorties
 *
 * dépendances
 * math.h
 */
void noiseblkv_c(int *n,int *typ,double *sig,double *mean,double *y_i,double *y_q)
{
 /*déclaration des variables*/
 int i;
 double rand1, rand2;
 double rand_m;
 double ampl, phase;

 /*récupération de la valeur de RAND_MAX*/
 rand_m=RAND_MAX;

 for(i=0;i<(*n);i++)
 {
  /*calcul rand1*/
  rand1=rand()/rand_m;
  /*test rand1*/
  while((rand1<=0)||(rand1>=1)) rand1=rand()/rand_m;

  /*calcul rand2*/
  rand2=rand()/rand_m;
  /*test rand2*/
  while((rand2<=0)||(rand2>=1)) rand2=rand()/rand_m;

  /*Calcul amplitude et phase*/
  ampl=sig[i]*sqrt(2*-log(rand1));
  phase=2*M_PI*rand2;

  /*Calcul y*/
  switch(*typ)
  {
   case 0 :
   {
    /*Calcul amplitude et phase*/
    ampl=sig[i]*sqrt(2*-log(rand1));
    phase=2*M_PI*rand2;
    y_i[i]=mean[i]+ampl*cos(phase);
    break;
   }

   case 1 :
   {
    /*Calcul amplitude et phase*/
    ampl=sig[i]*sqrt(2*-log(rand1));
    phase=2*M_PI*rand2;
    y_q[i]=mean[i]+ampl*sin(phase);
    break;
   }

   case 2 :
   {
    /*Calcul amplitude et phase*/
    ampl=sig[i]*sqrt(-log(rand1));
    phase=2*M_PI*rand2;
    y_i[i]=mean[i]+ampl*cos(phase);
    y_q[i]=mean[i]+ampl*sin(phase);
    break;
   }
  }
 }
 return;
}

/* noiseiq_c routine de calcul d'échantillons bruités par la méthode "Box Muller Law"
 *
 * Entrées :
 * n   : dimension 1 des matrices de sorties (scalaire)
 * m   : dimension 2 des matrices de sorties (scalaire)
 * sig  : variance (scalaire)
 * mean : moyenne (scalaire)
 * Sorties :
 * i_c : matrice des composantes I
 * i_q : matrice des composantes Q
 *
 * dépendances
 * math.h
 */

void noiseiq_c(int *n,int *m,double *sig,double *mean,double *i_c,double *i_q)
{
 /*déclaration des variables*/
 int i,l;
 double rand1, rand2;
 double rand_m;
 double ampl, phase;

 /*récupération de la valeur de RAND_MAX*/
 rand_m=RAND_MAX;

 for(l=0;l<(*m);l++) {
   for(i=0;i<(*n);i++) {
     /*calcul rand1*/
     rand1=rand()/rand_m;
     /*test rand1*/
     while((rand1<=0)||(rand1>=1)) rand1=rand()/rand_m;

     /*calcul rand2*/
     rand2=rand()/rand_m;
     /*test rand2*/
     while((rand2<=0)||(rand2>=1)) rand2=rand()/rand_m;

     /*Calcul amplitude et phase*/
     ampl=(*sig)*sqrt(-log(rand1));
     phase=2*M_PI*rand2;

     /*Calcul i_c et i_q*/
     i_c[(*n)*l+i]=(*mean)+ampl*cos(phase);
     i_q[(*n)*l+i]=(*mean)+ampl*sin(phase);
   }
 }
 return;
}

Auteurs

A. Layec