|
Packit |
c948fe |
/*
|
|
Packit |
c948fe |
* Copyright (C) 2004 Steve Harris
|
|
Packit |
c948fe |
*
|
|
Packit |
c948fe |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
c948fe |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
c948fe |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
c948fe |
* (at your option) any later version.
|
|
Packit |
c948fe |
*
|
|
Packit |
c948fe |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
c948fe |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
c948fe |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
c948fe |
* GNU General Public License for more details.
|
|
Packit |
c948fe |
*
|
|
Packit |
c948fe |
* $Id: distortion-test.c $
|
|
Packit |
c948fe |
*/
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
#include <unistd.h>
|
|
Packit |
c948fe |
#include <string.h>
|
|
Packit |
c948fe |
#include <math.h>
|
|
Packit |
c948fe |
#include <stdio.h>
|
|
Packit |
c948fe |
#include <stdint.h>
|
|
Packit |
c948fe |
#include <stdlib.h>
|
|
Packit |
c948fe |
#include <complex.h>
|
|
Packit |
c948fe |
#include <fftw3.h>
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
#include "gdither.h"
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
#define SIZE 16384
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
#ifndef M_PI
|
|
Packit |
c948fe |
#define M_PI 3.1415926
|
|
Packit |
c948fe |
#endif
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
float in[SIZE];
|
|
Packit |
c948fe |
int32_t out[SIZE];
|
|
Packit |
c948fe |
double sin_tbl[SIZE];
|
|
Packit |
c948fe |
double sinless_tbl[SIZE];
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
void mkdata(GDitherType dt, const char *desc);
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
int main(int argc, char *argv[])
|
|
Packit |
c948fe |
{
|
|
Packit |
c948fe |
int i;
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
for (i=0; i
|
|
Packit |
c948fe |
sin_tbl[i] = sin((double)i * M_PI * 0.05) * 0.01;
|
|
Packit |
c948fe |
in[i] = (float)sin_tbl[i];
|
|
Packit |
c948fe |
}
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
//mkdata(GDitherNone, "no dithering");
|
|
Packit |
c948fe |
//mkdata(GDitherRect, "rectangular dithering");
|
|
Packit |
c948fe |
//mkdata(GDitherTri, "triangular dithering");
|
|
Packit |
c948fe |
mkdata(GDitherShaped, "noise-shaped dithering");
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
return 0;
|
|
Packit |
c948fe |
}
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
void mkdata(GDitherType dt, const char *desc)
|
|
Packit |
c948fe |
{
|
|
Packit |
c948fe |
int i;
|
|
Packit |
c948fe |
GDither *ds = gdither_new(dt, 1, 16, 0);
|
|
Packit |
c948fe |
int16_t out16[SIZE];
|
|
Packit |
c948fe |
fftw_plan plan_rc;
|
|
Packit |
c948fe |
fftw_complex *freq = fftw_malloc(sizeof(fftw_complex) * (SIZE + 1));
|
|
Packit |
c948fe |
double amp;
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
plan_rc = fftw_plan_dft_r2c_1d(SIZE, sinless_tbl, freq, FFTW_PRESERVE_INPUT);
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
gdither_runf(ds, 0, SIZE, in, out16);
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
for (i=0; i
|
|
Packit |
c948fe |
sinless_tbl[i] = ((double)out16[i] / 32768.0) - sin_tbl[i];
|
|
Packit |
c948fe |
sinless_tbl[i] *= -0.5 * cos(2.0f * M_PI * (float) i /
|
|
Packit |
c948fe |
(float) SIZE) + 0.5;
|
|
Packit |
c948fe |
}
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
fftw_execute(plan_rc);
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
for (i=2; i
|
|
Packit |
c948fe |
amp = 20.0 * log10(cabs(freq[i]));
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
printf("%f\t%g\n", 48000.0 / (double)SIZE * (double)i, amp);
|
|
Packit |
c948fe |
}
|
|
Packit |
c948fe |
}
|
|
Packit |
c948fe |
|
|
Packit |
c948fe |
/* vi:set ts=8 sts=4 sw=4: */
|