Exemplo n.º 1
0
// samples should be u-law encoded
void modem_decode(buffer_t *input, buffer_t *output)
{
    static int phase = -1;
    int max_idx;
    size_t i;
    int16_t max_mag;
    int16_t samples[SAMPLES_PER_ITER];

    for (i = 0; i < SAMPLES_PER_ITER; i++)
    {
        uint8_t byte;
        buffer_read_bytes(input, &byte, 1);
        samples[i] = comp_decode(byte) * 4;
    }

    // apply band-pass filter to remove noise
    bandpass_filter(samples);

    // apply convolution with delay line
    convolution(samples);

    // apply low-pass filter to remove high-frequency artifacts
    lowpass_filter(samples);

    // try and find the middle of the phase
    if (phase == -1)
    {
        max_mag = 0;
        max_idx = 0;
        for (i = 0; i < SAMPLES_PER_ITER; i++)
        {
            if (samples[i] > max_mag)
            {
                max_mag = samples[i];
                max_idx = i;
            }
            else if (-samples[i] > max_mag)
            {
                max_mag = -samples[i];
                max_idx = i;
            }
        }
        phase = (max_idx) % (SAMPLES_PER_BIT / 2);
    }

    // sample at baudrate to get output
    for (i = phase; i < SAMPLES_PER_ITER; i += SAMPLES_PER_BIT)
    {
        int avg = 0;
        int j;
        for (j = 0; j < SAMPLES_PER_BIT / 4; j++)
            avg += samples[i + j]; //< 0 ? -1 : 1;
        avg /= SAMPLES_PER_BIT / 4;
        int bit = avg < 0 ? 1 : 0;
        buffer_write_bit(output, bit);
    }
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    int i, j, m, n;
	double			tempo_kernel;
    double			tempo_total;
    char			*output_filename;
	float			*image_amplitudes;
	float           (*x)[2];  
	float           (*X)[2]; 
	
	pgm_t			ipgm, opgm;   
	image_file_t	*image_filename;
    timer_reset();
    timer_start();

    if (argc < 2)
    {
        printf("**Erro: parametros de entrada invalidos");
        exit(EXIT_FAILURE);
    }

    image_filename = (image_file_t *) malloc(sizeof(image_file_t));
    split_image_filename(image_filename, argv[1]);
    output_filename = (char *) malloc(40*sizeof(char));
    sprintf(output_filename, "%d.%d.%s.%s.%s", image_filename->res, image_filename->num, ENV_TYPE, APP_TYPE, EXTENSAO);

    if( ler_pgm(&ipgm, argv[1]) == -1)
        exit(EXIT_FAILURE);

	n = ipgm.width;
	m = (int)(log((double)n)/log(2.0));
	x = malloc(2 * n * n * sizeof(float));
	X = malloc(2 * n * n * sizeof(float));
		
	opgm.width = n;
    opgm.height = n;
    
	for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            x[i*n + j][0] = (float) ipgm.buf[i*n + j];
			x[i*n + j][1] = (float) 0;
        }
    }

	/* Check that n = 2^m for some integer m >= 1. */
	if (n >= 2) {
		i = n;
		while(i==2*(i/2)) i = i/2;  /* While i is even, factor out a 2. */
	}  /* For n >=2, we now have N = 2^n iff i = 1. */
	if (n < 2 || i != 1) {
		  printf(" %d deve ser um inteiro tal que n = 2^m , para m >= 1", n);
		  exit(EXIT_FAILURE);
	}
	
	timer_stop();
    tempo_total = get_elapsed_time();

    //====== Performance Test - start =======================================
    timer_reset();
    timer_start();
	j = 0;
	j = n*n;
	
    //fft direta
	fft(j, x, X);
	// filtro passa baixa
	lowpass_filter(X, n);
	//fft inversa
	for(i=0; i<j; i++) x[i][0] = x[i][1] = 0;
	ifft(j, x, X);
   
	timer_stop();
    tempo_kernel = get_elapsed_time();

    tempo_total += tempo_kernel;
    //====== Performance Test - end ============================================

    save_log_cpu(image_filename, tempo_kernel, tempo_total, LOG_NAME);
	
	image_amplitudes = (float*)malloc(n*n*sizeof(float));
	 for (i=0; i < n; i++) {
        for (j=0; j < n; j++) {
            image_amplitudes[i*n + j] = (float) (AMP(x[i*n + j][0], x[i*n + j][1]));
        }
    }
	
	normalizar_pgm(&opgm, image_amplitudes);
    escrever_pgm(&opgm, output_filename);
    free(x);
	free(X);
	destruir_pgm(&ipgm);
    destruir_pgm(&opgm);
    free(image_filename);
    free(output_filename);
    free(image_amplitudes); 
   _CrtDumpMemoryLeaks();
    return 0;
}