libdspl-2.0/dspl/src/dft.c

136 wiersze
2.8 KiB
C
Czysty Zwykły widok Historia

2018-03-12 20:48:32 +00:00
/*
* Copyright (c) 2015-2019 Sergey Bakhurin
2018-03-12 20:48:32 +00:00
* Digital Signal Processing Library [http://dsplib.org]
*
* This file is part of libdspl-2.0.
2018-10-24 17:39:51 +00:00
*
2018-03-12 20:48:32 +00:00
* is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DSPL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <math.h>
#include "dspl.h"
2018-10-24 17:39:51 +00:00
/******************************************************************************
2018-03-12 20:48:32 +00:00
Real vector DFT
2018-10-24 17:39:51 +00:00
*******************************************************************************/
2018-03-12 20:48:32 +00:00
int DSPL_API dft(double* x, int n, complex_t *y)
{
2018-10-24 17:39:51 +00:00
int k;
int m;
double divn;
double phi;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
if(!x || !y)
return ERROR_PTR;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
if(n<1)
return ERROR_SIZE;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
divn = 1.0 / (double)n;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
for(k = 0; k < n; k++)
{
RE(y[k]) = IM(y[k]) = 0.0;
for(m = 0; m < n; m++)
2018-03-12 20:48:32 +00:00
{
2018-10-24 17:39:51 +00:00
phi = -M_2PI * divn * (double)k * (double)m;
RE(y[k]) += x[m] * cos(phi);
IM(y[k]) += x[m] * sin(phi);
}
}
return RES_OK;
2018-03-12 20:48:32 +00:00
}
2018-10-24 17:39:51 +00:00
/******************************************************************************
2018-03-12 20:48:32 +00:00
Complex vector DFT
2018-10-24 17:39:51 +00:00
*******************************************************************************/
2018-03-12 20:48:32 +00:00
int DSPL_API dft_cmplx(complex_t* x, int n, complex_t *y)
{
2018-10-24 17:39:51 +00:00
int k;
int m;
double divn;
double phi;
complex_t e;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
if(!x || !y)
return ERROR_PTR;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
if(n<1)
return ERROR_SIZE;
2018-03-12 20:48:32 +00:00
2018-10-24 17:39:51 +00:00
divn = 1.0 / (double)n;
for(k = 0; k < n; k++)
{
RE(y[k]) = IM(y[k]) = 0.0;
for(m = 0; m < n; m++)
2018-03-12 20:48:32 +00:00
{
2018-10-24 17:39:51 +00:00
phi = -M_2PI * divn * (double)k * (double)m;
RE(e) = cos(phi);
IM(e) = sin(phi);
RE(y[k]) += CMRE(x[m], e);
IM(y[k]) += CMIM(x[m], e);
}
}
return RES_OK;
2018-03-12 20:48:32 +00:00
}
2018-05-05 16:51:32 +00:00
2018-10-24 17:39:51 +00:00
/******************************************************************************
2018-05-05 16:51:32 +00:00
Complex vector DFT
2018-10-24 17:39:51 +00:00
*******************************************************************************/
2018-05-05 16:51:32 +00:00
int DSPL_API idft_cmplx(complex_t* x, int n, complex_t *y)
{
2018-10-24 17:39:51 +00:00
int k;
int m;
double divn;
double phi;
complex_t e;
if(!x || !y)
return ERROR_PTR;
2018-05-05 16:51:32 +00:00
2018-10-24 17:39:51 +00:00
if(n<1)
return ERROR_SIZE;
2018-05-05 16:51:32 +00:00
2018-10-24 17:39:51 +00:00
divn = 1.0 / (double)n;
2018-05-05 16:51:32 +00:00
2018-10-24 17:39:51 +00:00
for(k = 0; k < n; k++)
{
RE(y[k]) = IM(y[k]) = 0.0;
for(m = 0; m < n; m++)
2018-05-05 16:51:32 +00:00
{
2018-10-24 17:39:51 +00:00
phi = M_2PI * divn * (double)k * (double)m;
RE(e) = cos(phi);
IM(e) = sin(phi);
RE(y[k]) += CMRE(x[m], e);
IM(y[k]) += CMIM(x[m], e);
}
RE(y[k]) /= (double)n;
IM(y[k]) /= (double)n;
}
return RES_OK;
2018-05-05 16:51:32 +00:00
}