aboutsummaryrefslogtreecommitdiffstats
path: root/Transceiver52M/arch/common/fft.c
blob: ed79d1305246e68e3657adcb5c753c368ef3a553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Fast Fourier transform
 *
 * Copyright (C) 2012 Tom Tsou <tom@tsou.cc>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 * See the COPYING file in the main directory for details.
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fftw3.h>

#include "fft.h"

struct fft_hdl {
	float *fft_in;
	float *fft_out;
	int len;
	fftwf_plan fft_plan;
};

/*! \brief Initialize FFT backend
 *  \param[in] reverse FFT direction
 *  \param[in] m FFT length
 *  \param[in] istride input stride count
 *  \param[in] ostride output stride count
 *  \param[in] in input buffer (FFTW aligned)
 *  \param[in] out output buffer (FFTW aligned)
 *  \param[in] ooffset initial offset into output buffer
 *
 * If the reverse is non-NULL, then an inverse FFT will be used. This is a
 * wrapper for advanced non-contiguous FFTW usage. See FFTW documentation for
 * further details.
 *
 *   http://www.fftw.org/doc/Advanced-Complex-DFTs.html
 *
 * It is currently unknown how the offset of the output buffer affects FFTW
 * memory alignment.
 */
struct fft_hdl *init_fft(int reverse, int m, int istride, int ostride,
			 float *in, float *out, int ooffset)
{
	int rank = 1;
	int n[] = { m };
	int howmany = istride;
	int idist = 1;
	int odist = 1;
	int *inembed = n;
	int *onembed = n;
	fftwf_complex *obuffer, *ibuffer;

	struct fft_hdl *hdl = (struct fft_hdl *) malloc(sizeof(struct fft_hdl));
	if (!hdl)
		return NULL;

	int direction = FFTW_FORWARD;
	if (reverse)
		direction = FFTW_BACKWARD;

	ibuffer = (fftwf_complex *) in;
	obuffer = (fftwf_complex *) out + ooffset;

	hdl->fft_in = in;
	hdl->fft_out = out;
	hdl->fft_plan = fftwf_plan_many_dft(rank, n, howmany,
					    ibuffer, inembed, istride, idist,
					    obuffer, onembed, ostride, odist,
					    direction, FFTW_MEASURE);
	return hdl;
}

void *fft_malloc(size_t size)
{
	return fftwf_malloc(size);
}

void fft_free(void *ptr)
{
	free(ptr);
}

/*! \brief Free FFT backend resources
 */
void free_fft(struct fft_hdl *hdl)
{
	fftwf_destroy_plan(hdl->fft_plan);
	free(hdl);
}

/*! \brief Run multiple DFT operations with the initialized plan
 *  \param[in] hdl handle to an intitialized fft struct
 *
 * Input and output buffers are configured with init_fft().
 */
int cxvec_fft(struct fft_hdl *hdl)
{
	fftwf_execute(hdl->fft_plan);
	return 0;
}