Пример #1
0
/* ------------------------------------ */
void test_gaussian_noise_computation(void)
/* ------------------------------------ */
{
    int i;
    int n = 10000;
    float32 x, sx, sxx;

    sx = 0.0f; sxx = 0.0f;
    for(i=0; i<n; i++) {
        x = gaussian_noise(0, 10);
        //printf("%10.4f\n", x);
        sx += x;
        sxx += x*x;
    }
    printf("average = %10.4f sigma = %10.4f\n", sx/n, calc_sigma(n, sx, sxx));

    sx = 0.0f; sxx = 0.0f;
    for(i=0; i<n; i++) {
        x = gaussian_noise(0, 20);
        //printf("%10.4f\n", x);
        sx += x;
        sxx += x*x;
    }
    printf("average = %10.4f sigma = %10.4f\n", sx/n, calc_sigma(n, sx, sxx));
}
Пример #2
0
// ---------------------------------------
void test_gaussian_noise_computation(void)
// ---------------------------------------
{
    // pour valider que le generateur aleatoire a bien
    // les bons parametres statistiques ...
    
    int i;
    int n = 1000000;
    float32 x, sx, sxx;
    
    puts("-------------------------------------");
    puts("-- test_gaussian_noise_computation --");
    puts("-------------------------------------");
    
    sx = 0.0f; sxx = 0.0f;
    for(i = 0; i < n; i++) {
        x = gaussian_noise(0, 10);
        //printf("%10.4f\n", x);
        sx += x;
        sxx += x*x;
    }
    printf("average = %10.3f sigma = %10.3f\n", sx/n, calc_sigma(n, sx, sxx));
    
    sx = 0.0f; sxx = 0.0f;
    for(i = 0; i < n; i++) {
        x = gaussian_noise(0, 20);
        //printf("%10.4f\n", x);
        sx += x;
        sxx += x*x;
    }
    printf("average = %10.3f sigma = %10.3f\n", sx/n, calc_sigma(n, sx, sxx));
}
Пример #3
0
void
update_pid_params(rl_data* data)
{
	data->variables.sigma_critical_deviation = 1 / (1 + exp(2 * data->variables.critic_value));
	double g_noise = gaussian_noise(0, data->variables.sigma_critical_deviation) * GAUSS_MULTIPLIER; //mean = 0// zero-mean and magntude = sigma_critical_deviation
	data->variables.pid_params[0] = data->variables.recomended_pid_params[0] + g_noise;
	g_noise = gaussian_noise(0, data->variables.sigma_critical_deviation) * GAUSS_MULTIPLIER; //mean = 0// zero-mean and magntude = sigma_critical_deviation
	data->variables.pid_params[1] = data->variables.recomended_pid_params[1] + g_noise;
	g_noise = gaussian_noise(0, data->variables.sigma_critical_deviation) * GAUSS_MULTIPLIER; //mean = 0// zero-mean and magntude = sigma_critical_deviation
	data->variables.pid_params[2] = data->variables.recomended_pid_params[2] + g_noise;
}
Пример #4
0
// OK
void test_gaussain_noise(){
    for(int i = 0 ; i < 10; ++i){
        for(int j = 0 ; j < 10; ++j){
            printf("%lf ",gaussian_noise());
        }
        printf("\n");
    }

}
Пример #5
0
/* -------------------------------------------------------------------------------------------- */
void gaussian_noise_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, float32 sigma, uint8 **Y)
/* -------------------------------------------------------------------------------------------- */
{
    int i, j;
    float32 x, y, g;
    
    for(i=i0; i<=i1; i++) {
        for(j=j0; j<=j1; j++) {
            x = X[i][j];
            g = gaussian_noise(0.0f, sigma);
            y = x + g;
            if(y<0.0f) y = 0.0f;
            if(y>255.0f) y = 255.0f;
            Y[i][j] = (uint8) y;
        }
    }
}
Пример #6
0
int main(int argc,char* argv[]){

    startRandom();

    // control SNR step
    const double s_snr = -0.5;
    const double step = 0.02;
    const int snr_size = 500;

    // control the certain Frame and the bp
    int frame = 2;
    int t_lvl = 0;
    double _snr = 0;
    if(argc >= 3 )
        _snr = strtod(argv[2],NULL);

    if(argc >= 4)
        frame = strtod(argv[3],NULL);

    if(argc >= 5)
        t_lvl = strtod(argv[4],NULL);

    // for video encode
    const int puncture = 0;                     // puncture or not
    const double rate = 1/(double)(2-puncture);       // code rate
    const double a = 1;                         // Fading amplitude. a=1 -> AWGN channel
    double EbN0,L_c,sigma;

    // for basical info
    char buffer[50];
    const int h = __HEIGHT, w = __WIDTH, f = frame+1 ;
    const int lm = h*w;
    const int lu = lm+(G_L-1);
    int ** G = getGenerator();
    double * Ly = (double*) malloc(sizeof(double)*2*lu);
    int*** Y = new3d<int>(f,h,w);
    int* map_out = (int*) malloc(sizeof(int)*lm);

    double* Lu = (double*) malloc(sizeof(double)*lu);
    for(int i=0; i<lu ;++i)
        Lu[i] = 0;

    // pstate, pout
    const int Ns = pow(2,G_L-1);
    int ** pout = new2d<int>(Ns,4);
    int ** pstate = new2d<int>(Ns,2) ;
    double* Le1 = (double*)malloc(sizeof(double)*lu);
    double* Le2 = (double*)malloc(sizeof(double)*lu);

    trellis(G,G_N,G_L,Ns,pout,pstate);

    // frame buffer
    int*** imgr_bp = new3d<int>(PXL,h,w);
    double** Lu_c = new2d<double>(PXL,lu);  //channel decoder output

    // buffer for Ia, Ie
    double* Le = (double*) malloc(sizeof(double)*lm);
    double* Ia_pc = (double*) malloc(sizeof(double)*snr_size);
    double* Ie_pc = (double*) malloc(sizeof(double)*snr_size);
    int idx = 0;
    double tmp_sum = 0, tmp ;

    // read YUV
    sprintf(buffer,"%s_cif.yuv",argv[1]);
    yuv_read(buffer,h,w,f,Y,NULL,NULL);

//  video_encode(Y,f,h,w,_snr,G,Ly,map_out) for frame and t_lvl
    EbN0 = pow(10,_snr/10);      // convert Eb/N0[dB] to normal number
    L_c = 4*a*EbN0*rate;           // reliability value of the channel
    sigma = 1/sqrt(2*rate*EbN0);   // standard deviation of AWGN noise

    int* x = (int*) malloc(sizeof(int)*2*lu);
    img2bp_frame(Y[frame],h,w,imgr_bp);
    random_sequence(0,lm-1,map_out);
    rsc_encode(G,G_L,imgr_bp[t_lvl],map_out,w,lm,1,x);

    for(int i = 0 ; i < 2*lu ; ++i)
        Ly[i] = 0.5*L_c*((2*x[i] - 1)+ sigma*gaussian_noise());

    printf("processing ...%2.2f%%\n",0);
    for(double snr = s_snr; idx < snr_size ; snr+=step, idx++ , tmp_sum=0){
        printf("\tprocessing ...%2.2f%%\n",100*(snr-s_snr)/step/snr_size);
        // encode
        EbN0 = pow(10,snr/10);      // convert Eb/N0[dB] to normal number
        L_c = 4*a*EbN0*rate;           // reliability value of the channel
        sigma = 1/sqrt(2*rate*EbN0);   // standard deviation of AWGN noise

//        img2bp_frame(Y[frame],h,w,imgr_bp);
        for(int i = 0 ; i < lm ; ++i){
            tmp = (2*imgr_bp[t_lvl][map_out[i]/w][map_out[i]%w] - 1);
            Lu[i] = 0.5*L_c*( tmp + sigma*gaussian_noise());
            tmp_sum+=log2(1 + exp(-Lu[i]*tmp));
        }

        Ia_pc[idx] = 1 - tmp_sum/lm;

        // decode
        computeLe(Lu,Le1,Le2,lu);
        logmap(Ns, lu, 1, Ly, Le1, Le2, pstate, pout, Lu_c[t_lvl]);

        for(int i = 0 ; i < lm ; ++i)
            Le[map_out[i]] = Lu_c[t_lvl][i] - Lu[i];

        tmp_sum = 0;
        for(int i=0; i < lm ; ++i)
            tmp_sum+=log2(1 + exp(-Le[i]*(2*imgr_bp[t_lvl][i/w][i%w]-1)));

        Ie_pc[idx] = 1 - tmp_sum/lm;

    }
    printf("\r100%% completed!\n");

    FILE *file = fopen("output/exit_curve_pc.txt","a+");

    fprintf(file,"============================\n%s:SNR=%lf, frame#%d,bp#%d\nIa=\n",argv[1],_snr,frame+1,t_lvl+1);
    for(int i = 0 ; i < snr_size ; ++i)
        fprintf(file,"%lf,",Ia_pc[i]);
    fprintf(file,"\nIe=\n");
    for(int i = 0 ; i < snr_size ; ++i)
        fprintf(file,"%lf,",Ie_pc[i]);
    fprintf(file,"\n\n");

    fclose(file);

    write_pc_for_matlab(Ia_pc,Ie_pc,snr_size);

    // free memory
    free(Ly);
    free(map_out);
    delete2d<int>(G);
    deleteY(Y);

    delete3d<int>(imgr_bp);
    delete2d<double>(Lu_c);
    delete2d<int>(pstate);
    delete2d<int>(pout);

    free(Le);
    free(Lu);
    free(Le1);
    free(Le2);

    free(Ia_pc);
    free(Ie_pc);
    free(x);
}
Пример #7
0
int main(int argc,char* argv[]){

    startRandom();

    // control SNR step
    const double s_snr = -1;
    const double step = 0.05;
    const int snr_size = 500;

    // control the certain Frame and the bp
    int frame = 2;

    if(argc >= 4)
        frame = strtod(argv[3],NULL);

    // for video encode
    const int puncture = 0;                     // puncture or not
    const double rate = 1/(double)(2-puncture);       // code rate
    const double a = 1;                         // Fading amplitude. a=1 -> AWGN channel
    double EbN0,L_c,sigma;

    // for basical info
    char buffer[50];
    const int h = __HEIGHT, w = __WIDTH, f = frame+2 ;
    const int lm = h*w;
    const int lu = lm+(G_L-1);
    int*** Y = new3d<int>(f,h,w);

    int** MV_prev = new2d<int>(2,lm/64,0);
    int** MV = new2d<int>(2,lm/64,0);

    double** Lu = new2d<double>(PXL,lu,0);
    double** Lu_prev = new2d<double>(PXL,lu,0);
    double** Lu_next = new2d<double>(PXL,lu,0);

    // frame buffer
    int*** imgr_bp = new3d<int>(PXL,h,w);
    int*** imgr_bp_prev = new3d<int>(PXL,h,w);
    int*** imgr_bp_next = new3d<int>(PXL,h,w);
    int*** img = new3d<int>(PXL,h,w);
    int*** img_prev = new3d<int>(PXL,h,w);
    int*** img_next = new3d<int>(PXL,h,w);
    int** imgr = new2d<int>(h,w);
    int** imgr_prev = new2d<int>(h,w);
    int** imgr_next = new2d<int>(h,w);

    double** Lu_c = new2d<double>(PXL,lu);  //channel decoder output

    // buffer for Ia, Ie
    double** Le = new2d<double>(PXL,lm); // source extrinsic informatiom
    double** Le_s_inter = new2d<double>(PXL,lm,0); // source extrinsic information
    double** Le_s_inter_next = new2d<double>(PXL,lm,0); // source extrinsic information
    double** Le_s_intra = new2d<double>(PXL,lm,0); // source extrinsic information
    double** Ia = new2d<double>(PXL,snr_size);
    double** Ie = new2d<double>(PXL,snr_size);
    int idx = 0;
    double tmp_sum = 0, tmp ;

    double* beta_t = (double*) malloc(sizeof(double)*PXL);
    double* beta_s = (double*) malloc(sizeof(double)*PXL);
    double* beta_t_prev = (double*) malloc(sizeof(double)*PXL);

    // read YUV
    sprintf(buffer,"%s_cif.yuv",argv[1]);
    yuv_read(buffer,h,w,f,Y,NULL,NULL);


    img2bp_frame(Y[frame],h,w,imgr_bp);
    img2bp_frame(Y[frame-1],h,w,imgr_bp_prev);
    img2bp_frame(Y[frame+1],h,w,imgr_bp_next);

    printf("processing ...  %2.2f%%",0);
    for(double snr = s_snr; idx < snr_size ; snr+=step, idx++ ){
        printf("\rprocessing ... %2.2f%%",100*(snr-s_snr)/step/snr_size);
        // encode
        EbN0 = pow(10,snr/10);      // convert Eb/N0[dB] to normal number
        L_c = 4*a*EbN0*rate;           // reliability value of the channel
        sigma = 1/sqrt(2*rate*EbN0);   // standard deviation of AWGN noise

        for(int t_lvl = 0 ; t_lvl <PXL ; ++t_lvl){
            tmp_sum = 0;
            for(int i = 0 ; i < lm ; ++i){
                tmp = (2*imgr_bp[t_lvl][i/w][i%w] - 1);
                Lu[t_lvl][i] = 0.5*L_c*( tmp + sigma*gaussian_noise());
                tmp_sum+=log2(1 + exp(-Lu[t_lvl][i]*tmp));

                tmp = (2*imgr_bp_prev[t_lvl][i/w][i%w] - 1);
                Lu_prev[t_lvl][i] = 0.5*L_c*( tmp + sigma*gaussian_noise());
            }

            Ia[t_lvl][idx] = 1 - tmp_sum/lm;
        }

        // decode

        for(int i = 0, j=0, t_lvl=0; i <h ; ++i)
            for(j = 0 ; j < w ; ++j)
                for(t_lvl=0 ; t_lvl < PXL ; ++t_lvl){
                    img[t_lvl][i][j] = ((Lu[t_lvl][j+i*w]>=0)?1:0);
                    img_prev[t_lvl][i][j] = ((Lu_prev[t_lvl][j+i*w]>=0)?1:0);
                }

        bin2dec_img(img,h,w,imgr);
        bin2dec_img(img_prev,h,w,imgr_prev);

        motionEstES(imgr,imgr_prev,h,w,8,5,MV_prev);

        intra_inter_beta_estimation(img,img_prev,MV_prev,beta_s,beta_t,h,w,8);

        motionComp(Lu_prev,MV_prev,h,w,8,Lu_c);
        mrf_siso_inter(Lu,Lu_c,beta_t,h,w,Le_s_inter,0);
        mrf_siso_intra(Lu,beta_s,h,w,Le_s_intra,0);

        for(int i = 0, t_lvl = 0 ; i < lm ; ++i)
            for(t_lvl = 0 ; t_lvl < PXL ; ++t_lvl)
                Le[t_lvl][i] = (Le_s_inter[t_lvl][i] + Le_s_intra[t_lvl][i]);  // Le_s_prev in the next frame

        // prepare for next frame decode

        // encode

        for(int t_lvl = 0 ; t_lvl <PXL ; ++t_lvl){
            for(int i = 0 ; i < lm ; ++i){
                tmp = (2*imgr_bp_next[t_lvl][i/w][i%w] - 1);
                Lu_next[t_lvl][i] = 0.5*L_c*( tmp + sigma*gaussian_noise());  // reuse as Lu_next
            }
        }

        for(int i = 0, j=0, t_lvl=0; i <h ; ++i)
            for(j = 0 ; j < w ; ++j)
                for(t_lvl=0 ; t_lvl < PXL ; ++t_lvl){
                    img_next[t_lvl][i][j] = ((Lu_next[t_lvl][j+i*w]>=0)?1:0);
                }

        bin2dec_img(img_next,h,w,imgr_next);

        motionEstES(imgr,imgr_next,h,w,8,5,MV);

        if(frame==1){
            intra_inter_beta_estimation(imgr_bp,imgr_bp_next,MV,beta_s,beta_t,h,w,8);
            for(int i = 0 ; i < PXL ; ++i)
                beta_t_prev[i] = 0;
        }
        else
            intra_inter2_beta_estimation(imgr_bp_next,imgr_bp,imgr_bp_prev,MV,beta_s,beta_t,beta_t_prev,h,w,8);

        motionComp(Lu_next,MV,h,w,8,Lu_c);
        mrf_siso_inter(Lu,Lu_c,beta_t,h,w,Le_s_inter_next,0);
        mrf_siso_intra(Lu,beta_s,h,w,Le_s_intra,0);

        for(int i = 0, t_lvl = 0 ; i < lm ; ++i)
            for(t_lvl = 0 ; t_lvl < PXL ; ++t_lvl)
                Le[t_lvl][i] = (Le_s_inter_next[t_lvl][i] + Le_s_intra[t_lvl][i] + Le_s_inter[t_lvl][i]*beta_t_prev[t_lvl]);


        // compute Ie
        for(int t_lvl = 0 , i = 0; t_lvl < PXL ; ++t_lvl){
            tmp_sum = 0;
            for(i=0; i < lm ; ++i){
                tmp_sum+=log2(1 + exp(-Le[t_lvl][i]*(2*imgr_bp[0][0][i+t_lvl*lm]-1)));

            }
            Ie[t_lvl][idx] = 1 - tmp_sum/lm;
        }


    }

    printf("\r100.00%% completed!\n");

    FILE *file = fopen("output/exit_curve_ps.txt","a+");

    for(int t_lvl = 0 ; t_lvl < PXL ; ++t_lvl){
        fprintf(file,"==== v2 ========================\n%s: frame#%d,bp#%d\nIa=\n",argv[1],frame+1,t_lvl+1);
        for(int i = 0 ; i < snr_size ; ++i)
            fprintf(file,"%lf,",Ia[t_lvl][i]);
        fprintf(file,"\nIe=\n");
        for(int i = 0 ; i < snr_size ; ++i)
            fprintf(file,"%lf,",Ie[t_lvl][i]);
        fprintf(file,"\n\n");

    }
    fclose(file);

    write_ps_for_matlab(Ia,Ie,snr_size);

    // free memory
    deleteY(Y);

    delete3d<int>(imgr_bp);
    delete3d<int>(imgr_bp_prev);
    delete3d<int>(imgr_bp_next);
    delete3d<int>(img_next);
    delete3d<int>(img_prev);
    delete3d<int>(img);
    delete2d<int>(imgr_next);
    delete2d<int>(imgr_prev);
    delete2d<int>(imgr);

    delete2d<double>(Lu_c);

    delete2d<double>(Lu_next);
    delete2d<double>(Lu);
    delete2d<double>(Lu_prev);

    delete2d<double>(Ia);
    delete2d<double>(Ie);

    delete2d<int>(MV_prev);
    delete2d<int>(MV);

    delete2d<double>(Le);
    delete2d<double>(Le_s_inter);
    delete2d<double>(Le_s_inter_next);
    delete2d<double>(Le_s_intra);

    free(beta_s);
    free(beta_t);
    free(beta_t_prev);
}
Пример #8
0
void jones_optical::postprocess(input& invals){
#ifdef gen_t_dat
    func_dat=fopen("python/grad_data2.out", "w");
    func_score = fopen("python/score_data2.out", "w");
#else
    func_dat=fopen("python/grad_data.out", "w");
    func_score = fopen("python/score_data.out", "w");
#endif
    stable_spectral_pde_1d_tmpl<comp>::postprocess(invals);

    if(dimension%2){
        err("System jones_optical requires an even dimension", "jones_optical::postprocess",
                "system/jones_optical.cpp", FATAL_ERROR);
    }
    int num_segments;
    invals.retrieve(num_segments, "num_jones_segments", this);
    if(num_segments < 0){
        err("Number of jones segments must be greater than or equal to zero",
                "jones_optical::postprocess", "system/jones_optical.cpp", FATAL_ERROR);
    }
    invals.retrieve(jones_int_dist, "jones_int_dist", this);
    if(jones_int_dist<0){
        err("The distance between jones segments, jones_int_dist,  must be greater than or equal to zero",
                "jones_optical::postprocess", "system/jones_optical.cpp", FATAL_ERROR);
    }

    memp.add(dimension, &nvec1);
    memp.add(nts, &help, &t, &kurtosis_help, &phold, &nvec2);
    double dt = 60.0/nts;
    gaussian_noise(ucur, dimension, 0, 0.2); 
    for(size_t i = 0; i < nts; i++){
        t[i] = dt*(i-nts/2.0);
    }
    for(size_t i = 0; i < nts; i++){
        ucur[i] = ucur[i+nts] = 1.00/cosh(t[i]/2.0);
        help[i] = _real(ucur[i]);
        nvec1[i] = ucur[i];
    }
    for(size_t i = 0; i < num_pulses; i++){
        fft(nvec1 + i*nts, nvec1 +1*nts, nts);
    }
    //generate variables for the jones matrices
    //create jones matrices
    std::string name_base = "jones_system_vars";
    std::string mat_base = "jones_system_matrices";

    for(int i = 0; i < num_segments; i++){
        std::vector<std::shared_ptr<variable> > vv(4);
        for(auto& val : vv){
            val = std::make_shared<variable>();
            val->setname(get_unique_name(name_base));
            val->holder=holder;
            val->parse("0.1");
#ifdef gen_t_dat
            val->set(0*2*3.1415*(rand()*1.0/RAND_MAX));
#else
            val->set(0);
#endif
            invals.insert_item(val);
            cont->addvar(val);
        }
        std::shared_ptr<jones_matrix> m = std::make_shared<jones_matrix>(get_unique_name(mat_base), i, holder);
        invals.insert_item(m);
        m->setup(vv);
        jones_matrices.push_back(m);
    }
}