0

我正在尝试从 matlab 改编算法。

我的算法:

#include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <cwp.h>

#define MAX_LINES   225000
#define MAX_SAMPLE  2500
#define LMAX                200000
#define LFILT               128
#define TamConv     2580
#define LENGTH_(x) (sizeof(x)/sizeof(x[0]))



int
main(argc, argv)
int argc;
char **argv;
{
    FILE        *fp;
    float   data[MAX_LINES], samples[MAX_SAMPLE], mexh[81], values;

    float   j, pi, mean_atraso, passo, total_passo;
    double  escala[] = {1, 2, 4, 8};
    size_t  escala_len,k,l;
    int i,m,n;
    long unsigned int tex;
    float   convol[TamConv],aux1[TamConv],aux2[TamConv];
    pi = M_PI;


    /*
    Leitura dos Dados 
    */
    /*
    if (argv[1] == NULL) {
        perror("Sem arquivo de texto!");
        return 1;
    }  */

    fp = fopen("exames.txt", "r");

    if (fp == NULL) {
        perror("Arquivo vazio!");
        return 1;
    }

    while((fscanf(fp, "%lf", &values) && (i < MAX_LINES))) {
        data[i] = values;

        if (i <= MAX_SAMPLE) {
            samples[i] = values;
            mean_atraso += values;
        }

        i++;
    }

    fclose(fp);

    printf("DATA: %lu \n", LENGTH_(data)); // Tamanho do arquivo

    /*
    Calcula a tal media mean_atraso
    */

    mean_atraso /= MAX_SAMPLE;

    for (i=0; i<=MAX_SAMPLE; i++) {
        samples[i] -= mean_atraso;
    }

    /*
    Chapéu Mexicano
    */
    escala_len = LENGTH_(escala);

    printf("%zu \n", escala_len);   

    i=0;
    while (i < escala_len) {
        /*
        Chapéu Mexicano
        */
        passo = (float)1/escala[i];
        total_passo = 10/passo;
        for(j=-5, k=0; j<5, k<total_passo; j+=passo,k++) {
            mexh[k] = 2.1741*(1/sqrt(2*pi)*(1-pow(j,2))*exp(-pow(j,2)/(2)));
        }

        /*
        Convolution
        */
//      conv(total_passo,mexh[0],mexh,MAX_SAMPLE,data[0],data,MAX_SAMPLE+total_passo,0,convol);
//      
        for(k=0; k<(MAX_SAMPLE+total_passo); k++) {
            if (k<MAX_SAMPLE)
                aux1[k]=data[k];
            else
                aux1[k]=0;

            if (k<total_passo)
                aux2[k]=mexh[k];
            else
                aux2[k]=0;

        }
            for(l=0;l<(2500+total_passo); l++) {
                convol[l]=0;
                convol[l] =convol[l]+ (aux1[l] * aux2[l]);


            }

        //derivada de primeira ordem
                for (i=1; i<MAX_SAMPLE; i++) 
                    convol[i-1]=convol[i]-convol[i-1];

        /*
        Hilbert
        */  

        hilbert(MAX_SAMPLE+total_passo, convol [],aux1 [] );




        i++;
    }

    return 0;
}

我想从这个库cwplib中实现卷积和希尔伯特

我尝试通过上面的这个算法来实现卷积,但结果与 Matlab 不同。

如果没有,我该如何以另一种方式或卷积实现希尔伯特?

我能怎么做?感谢任何帮助。

4

0 回答 0