gaus_pdf.cpp

来自「Digital filter designer s handbook C++ c」· C++ 代码 · 共 104 行

CPP
104
字号
//
//  File = gaus_pdf.cpp
//

#include <math.h>
#include <fstream.h>
#include "uni_rand.h"
#include "gaus_pdf.h"
#include "misdefs.h"

extern ofstream PdfFile;
extern ofstream CdfFile;
 
void GaussianPdf( double sigma, 
                     double mu, 
                     int num_pts, 
                     double max_x)
{
 double two_sig_sqrd;
 double scaling_factor;
 double xx,xx_delta;
 double pdf_val;
 int i;
 
 two_sig_sqrd = 2.0*sigma*sigma;
 scaling_factor = 1.0/(sigma*sqrt(TWO_PI));
 xx_delta = 2.0 * max_x/(num_pts-1); 
 
 //====================================
 // special histogram stuff
 int hist_indx;
 int pts_per_bar;
 double rel_peak_val;
 double hist_val, histogram_value[16];
 pts_per_bar = num_pts/16;
 rel_peak_val = (0.99) * scaling_factor * 
                exp(-(mu*mu)/two_sig_sqrd)/0.4;
 histogram_value[0] = 0.0;
 histogram_value[1] = 0.01608637 * rel_peak_val;
 histogram_value[2] = 0.02895547 * rel_peak_val;
 histogram_value[3] = 0.11582187 * rel_peak_val;
 histogram_value[4] = 0.17051553 * rel_peak_val;
 histogram_value[5] = 0.28955468 * rel_peak_val;
 histogram_value[6] = 0.35390016 * rel_peak_val;
 histogram_value[7] = 0.40859382 * rel_peak_val;
 histogram_value[8] = 0.39250745 * rel_peak_val;
 histogram_value[9] = 0.28312013 * rel_peak_val;
 histogram_value[10] = 0.21555737 * rel_peak_val;
 histogram_value[11] = 0.14799461 * rel_peak_val;
 histogram_value[12] = 0.10938732 * rel_peak_val;
 histogram_value[13] = 0.05147639 * rel_peak_val;
 histogram_value[14] = 0.00643455 * rel_peak_val;
 histogram_value[15] = 0.0;
 //------------------------------------

 for(i=0; i<num_pts; i++)
   {
    xx = xx_delta * (i - (num_pts-1)/2);
    pdf_val = scaling_factor * exp(-(xx-mu)*(xx-mu)/two_sig_sqrd);
    //PdfFile << xx << ", " << pdf_val << endl;
    //====================================
    // special histogram stuff
    hist_indx = i/pts_per_bar;
    if( (i%pts_per_bar) == 0)
      {
      hist_val = 0.0;
      }
    else
      {
      hist_val = histogram_value[hist_indx];
      }
    PdfFile << xx << ", " << pdf_val << ", " 
            << hist_val << endl;
    //------------------------------------
   }
 return;
}

void GaussianCdf( double sigma, 
                  double mu, 
                  int num_pts, 
                  double max_x)
{
 double two_sig_sqrd;
 double scaling_factor;
 double xx,xx_delta;
 double pdf_val, cdf_val, cdf_sum;
 int i;
 
 two_sig_sqrd = 2.0*sigma*sigma;
 scaling_factor = 1.0/(sigma*sqrt(TWO_PI));
 xx_delta = 2.0 * max_x/(num_pts-1); 
 
 for(i=0; i<num_pts; i++)
   {
    xx = xx_delta * (i - (num_pts-1)/2);
    pdf_val = scaling_factor * exp(-(xx-mu)*(xx-mu)/two_sig_sqrd);
    cdf_sum += pdf_val;
    cdf_val = cdf_sum * xx_delta;
    CdfFile << xx << ", " << cdf_val << endl;
   }
 return;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?