Skip to content

Commit d85e607

Browse files
committed
additional cpp convenience wrappers for fft and ifft
1 parent f254dff commit d85e607

File tree

2 files changed

+163
-55
lines changed

2 files changed

+163
-55
lines changed

include/af/signal.h

Lines changed: 103 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,88 +85,127 @@ AFAPI array fft2(const array& in, double norm_factor, dim_type odim0=0, dim_type
8585
AFAPI array fft3(const array& in, double norm_factor, dim_type odim0=0, dim_type odim1=0, dim_type odim2=0);
8686

8787
/**
88-
C++ Interface for inverse fast fourier transform on one dimensional data
88+
C++ Interface for fast fourier transform on one dimensional data
89+
90+
This version of fft function uses a default norm_factor paramter that is calculated internally
91+
based on the input data.
8992
9093
\param[in] in is the input array
91-
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
9294
\param[in] odim0 is the length of output data - used to either truncate or pad the input data
9395
\return the transformed array
9496
95-
\ingroup signal_func_ifft
97+
\ingroup signal_func_fft
9698
*/
97-
AFAPI array ifft(const array& in, double norm_factor, dim_type odim0=0);
99+
AFAPI array fft(const array& in, dim_type odim0);
98100

99101
/**
100-
C++ Interface for inverse fast fourier transform on two dimensional data
102+
C++ Interface for fast fourier transform on two dimensional data
103+
104+
This version of fft function uses a default norm_factor paramter that is calculated internally
105+
based on the input data.
101106
102107
\param[in] in is the input array
103-
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
104108
\param[in] odim0 is the length of output data along 0th dimension - used to either truncate/pad the input
105109
\param[in] odim1 is the length of output data along 1st dimension - used to either truncate/pad the input
106110
\return the transformed array
107111
108-
\ingroup signal_func_ifft2
112+
\ingroup signal_func_fft2
109113
*/
110-
AFAPI array ifft2(const array& in, double norm_factor, dim_type odim0=0, dim_type odim1=0);
114+
AFAPI array fft2(const array& in, dim_type odim0=0, dim_type odim1=0);
111115

112116
/**
113-
C++ Interface for inverse fast fourier transform on three dimensional data
117+
C++ Interface for fast fourier transform on three dimensional data
118+
119+
This version of fft function uses a default norm_factor paramter that is calculated internally
120+
based on the input data.
114121
115122
\param[in] in is the input array
116-
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
117123
\param[in] odim0 is the length of output data along 0th dimension - used to either truncate/pad the input
118124
\param[in] odim1 is the length of output data along 1st dimension - used to either truncate/pad the input
119125
\param[in] odim2 is the length of output data along 2nd dimension - used to either truncate/pad the input
120126
\return the transformed array
121127
122-
\ingroup signal_func_ifft3
128+
\ingroup signal_func_fft3
123129
*/
124-
AFAPI array ifft3(const array& in, double norm_factor, dim_type odim0=0, dim_type odim1=0, dim_type odim2=0);
130+
AFAPI array fft3(const array& in, dim_type odim0=0, dim_type odim1=0, dim_type odim2=0);
125131

126132
/**
127-
C++ Interface for fast fourier transform on one dimensional data
133+
C++ Interface for fast fourier transform on any(1d, 2d, 3d) dimensional data
134+
135+
\param[in] in is the input array
136+
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
137+
\param[in] outDims is an object of \ref dim4 that has the output array dimensions - used to either truncate or pad the input data
138+
\return the transformed array
139+
140+
\ingroup signal_func_fft
141+
*/
142+
AFAPI array fft(const array& in, double norm_factor, const dim4 outDims);
143+
144+
/**
145+
C++ Interface for fast fourier transform on any(1d, 2d, 3d) dimensional data
128146
129147
This version of fft function uses a default norm_factor paramter that is calculated internally
130148
based on the input data.
131149
132150
\param[in] in is the input array
133-
\param[in] odim0 is the length of output data - used to either truncate or pad the input data
151+
\param[in] outDims is an object of \ref dim4 that has the output array dimensions - used to either truncate or pad the input data
134152
\return the transformed array
135153
136154
\ingroup signal_func_fft
137155
*/
138-
AFAPI array fft(const array& in, dim_type odim0=0);
156+
AFAPI array fft(const array& in, const dim4 outDims);
139157

140158
/**
141-
C++ Interface for fast fourier transform on two dimensional data
159+
C++ Interface for fast fourier transform on any(1d, 2d, 3d) dimensional data
142160
143161
This version of fft function uses a default norm_factor paramter that is calculated internally
144162
based on the input data.
145163
146164
\param[in] in is the input array
165+
\return the transformed array
166+
167+
\ingroup signal_func_fft
168+
*/
169+
AFAPI array fft(const array& in);
170+
171+
/**
172+
C++ Interface for inverse fast fourier transform on one dimensional data
173+
174+
\param[in] in is the input array
175+
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
176+
\param[in] odim0 is the length of output data - used to either truncate or pad the input data
177+
\return the transformed array
178+
179+
\ingroup signal_func_ifft
180+
*/
181+
AFAPI array ifft(const array& in, double norm_factor, dim_type odim0=0);
182+
183+
/**
184+
C++ Interface for inverse fast fourier transform on two dimensional data
185+
186+
\param[in] in is the input array
187+
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
147188
\param[in] odim0 is the length of output data along 0th dimension - used to either truncate/pad the input
148189
\param[in] odim1 is the length of output data along 1st dimension - used to either truncate/pad the input
149190
\return the transformed array
150191
151-
\ingroup signal_func_fft2
192+
\ingroup signal_func_ifft2
152193
*/
153-
AFAPI array fft2(const array& in, dim_type odim0=0, dim_type odim1=0);
194+
AFAPI array ifft2(const array& in, double norm_factor, dim_type odim0=0, dim_type odim1=0);
154195

155196
/**
156-
C++ Interface for fast fourier transform on three dimensional data
157-
158-
This version of fft function uses a default norm_factor paramter that is calculated internally
159-
based on the input data.
197+
C++ Interface for inverse fast fourier transform on three dimensional data
160198
161199
\param[in] in is the input array
200+
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
162201
\param[in] odim0 is the length of output data along 0th dimension - used to either truncate/pad the input
163202
\param[in] odim1 is the length of output data along 1st dimension - used to either truncate/pad the input
164203
\param[in] odim2 is the length of output data along 2nd dimension - used to either truncate/pad the input
165204
\return the transformed array
166205
167-
\ingroup signal_func_fft3
206+
\ingroup signal_func_ifft3
168207
*/
169-
AFAPI array fft3(const array& in, dim_type odim0=0, dim_type odim1=0, dim_type odim2=0);
208+
AFAPI array ifft3(const array& in, double norm_factor, dim_type odim0=0, dim_type odim1=0, dim_type odim2=0);
170209

171210
/**
172211
C++ Interface for inverse fast fourier transform on one dimensional data
@@ -180,7 +219,7 @@ AFAPI array fft3(const array& in, dim_type odim0=0, dim_type odim1=0, dim_type o
180219
181220
\ingroup signal_func_ifft
182221
*/
183-
AFAPI array ifft(const array& in, dim_type odim0=0);
222+
AFAPI array ifft(const array& in, dim_type odim0);
184223

185224
/**
186225
C++ Interface for inverse fast fourier transform on two dimensional data
@@ -213,6 +252,45 @@ AFAPI array ifft2(const array& in, dim_type odim0=0, dim_type odim1=0);
213252
*/
214253
AFAPI array ifft3(const array& in, dim_type odim0=0, dim_type odim1=0, dim_type odim2=0);
215254

255+
/**
256+
C++ Interface for inverse fast fourier transform on any(1d, 2d, 3d) dimensional data
257+
258+
\param[in] in is the input array
259+
\param[in] norm_factor is the normalization factor with which the input is scaled before the transformation is applied
260+
\param[in] outDims is an object of \ref dim4 that has the output array dimensions - used to either truncate or pad the input data
261+
\return the transformed array
262+
263+
\ingroup signal_func_fft
264+
*/
265+
AFAPI array ifft(const array& in, double norm_factor, const dim4 outDims);
266+
267+
/**
268+
C++ Interface for inverse fast fourier transform on any(1d, 2d, 3d) dimensional data
269+
270+
This version of fft function uses a default norm_factor paramter that is calculated internally
271+
based on the input data.
272+
273+
\param[in] in is the input array
274+
\param[in] outDims is an object of \ref dim4 that has the output array dimensions - used to either truncate or pad the input data
275+
\return the transformed array
276+
277+
\ingroup signal_func_fft
278+
*/
279+
AFAPI array ifft(const array& in, const dim4 outDims);
280+
281+
/**
282+
C++ Interface for inverse fast fourier transform on any(1d, 2d, 3d) dimensional data
283+
284+
This version of fft function uses a default norm_factor paramter that is calculated internally
285+
based on the input data.
286+
287+
\param[in] in is the input array
288+
\return the transformed array
289+
290+
\ingroup signal_func_fft
291+
*/
292+
AFAPI array ifft(const array& in);
293+
216294
/**
217295
C++ Interface for convolution any(one through three) dimensional data
218296

src/api/cpp/fft.cpp

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace af
1414
{
1515

16+
1617
array fft(const array& in, double norm_factor, dim_type odim0)
1718
{
1819
af_array out = 0;
@@ -34,48 +35,61 @@ array fft3(const array& in, double norm_factor, dim_type odim0, dim_type odim1,
3435
return array(out);
3536
}
3637

37-
array ifft(const array& in, double norm_factor, dim_type odim0)
38+
array fft(const array& in, dim_type odim0)
3839
{
39-
af_array out = 0;
40-
AF_THROW(af_ifft(&out, in.get(), norm_factor, odim0));
41-
return array(out);
40+
return fft(in, 1.0, odim0);
4241
}
4342

44-
array ifft2(const array& in, double norm_factor, dim_type odim0, dim_type odim1)
43+
array fft2(const array& in, dim_type odim0, dim_type odim1)
4544
{
46-
af_array out = 0;
47-
AF_THROW(af_ifft2(&out, in.get(), norm_factor, odim0, odim1));
48-
return array(out);
45+
return fft2(in, 1.0, odim0, odim1);
4946
}
5047

51-
array ifft3(const array& in, double norm_factor, dim_type odim0, dim_type odim1, dim_type odim2)
48+
array fft3(const array& in, dim_type odim0, dim_type odim1, dim_type odim2)
5249
{
53-
af_array out = 0;
54-
AF_THROW(af_ifft3(&out, in.get(), norm_factor, odim0, odim1, odim2));
55-
return array(out);
50+
return fft3(in, 1.0, odim0, odim1, odim2);
5651
}
5752

58-
array fft(const array& in, dim_type odim0)
53+
array fft(const array& in, double norm_factor, const dim4 outDims)
54+
{
55+
array temp;
56+
switch(in.dims().ndims()) {
57+
case 1: temp = fft(in, norm_factor, outDims[0]); break;
58+
case 2: temp = fft2(in, norm_factor, outDims[0], outDims[1]); break;
59+
case 3: temp = fft3(in, norm_factor, outDims[0], outDims[1], outDims[2]); break;
60+
default: AF_THROW(AF_ERR_NOT_SUPPORTED);
61+
}
62+
return temp;
63+
}
64+
65+
array fft(const array& in, const dim4 outDims)
66+
{
67+
return fft(in, 1.0, outDims);
68+
}
69+
70+
array fft(const array& in)
71+
{
72+
return fft(in, 1.0, dim4(0,0,0,0));
73+
}
74+
75+
array ifft(const array& in, double norm_factor, dim_type odim0)
5976
{
60-
double norm_factor = 1.0;
6177
af_array out = 0;
62-
AF_THROW(af_fft(&out, in.get(), norm_factor, odim0));
78+
AF_THROW(af_ifft(&out, in.get(), norm_factor, odim0));
6379
return array(out);
6480
}
6581

66-
array fft2(const array& in, dim_type odim0, dim_type odim1)
82+
array ifft2(const array& in, double norm_factor, dim_type odim0, dim_type odim1)
6783
{
68-
double norm_factor = 1.0;
6984
af_array out = 0;
70-
AF_THROW(af_fft2(&out, in.get(), norm_factor, odim0, odim1));
85+
AF_THROW(af_ifft2(&out, in.get(), norm_factor, odim0, odim1));
7186
return array(out);
7287
}
7388

74-
array fft3(const array& in, dim_type odim0, dim_type odim1, dim_type odim2)
89+
array ifft3(const array& in, double norm_factor, dim_type odim0, dim_type odim1, dim_type odim2)
7590
{
76-
double norm_factor = 1.0;
7791
af_array out = 0;
78-
AF_THROW(af_fft3(&out, in.get(), norm_factor, odim0, odim1, odim2));
92+
AF_THROW(af_ifft3(&out, in.get(), norm_factor, odim0, odim1, odim2));
7993
return array(out);
8094
}
8195

@@ -84,9 +98,7 @@ array ifft(const array& in, dim_type odim0)
8498
const dim4 dims = in.dims();
8599
dim_type dim0 = odim0==0 ? dims[0] : odim0;
86100
double norm_factor = 1.0/dim0;
87-
af_array out = 0;
88-
AF_THROW(af_ifft(&out, in.get(), norm_factor, odim0));
89-
return array(out);
101+
return ifft(in, norm_factor, odim0);
90102
}
91103

92104
array ifft2(const array& in, dim_type odim0, dim_type odim1)
@@ -95,9 +107,7 @@ array ifft2(const array& in, dim_type odim0, dim_type odim1)
95107
dim_type dim0 = odim0==0 ? dims[0] : odim0;
96108
dim_type dim1 = odim1==0 ? dims[1] : odim1;
97109
double norm_factor = 1.0/(dim0*dim1);
98-
af_array out = 0;
99-
AF_THROW(af_ifft2(&out, in.get(), norm_factor, odim0, odim1));
100-
return array(out);
110+
return ifft2(in, norm_factor, odim0, odim1);
101111
}
102112

103113
array ifft3(const array& in, dim_type odim0, dim_type odim1, dim_type odim2)
@@ -107,9 +117,29 @@ array ifft3(const array& in, dim_type odim0, dim_type odim1, dim_type odim2)
107117
dim_type dim1 = odim1==0 ? dims[1] : odim1;
108118
dim_type dim2 = odim2==0 ? dims[2] : odim2;
109119
double norm_factor = 1.0/(dim0*dim1*dim2);
110-
af_array out = 0;
111-
AF_THROW(af_ifft3(&out, in.get(), norm_factor, odim0, odim1, odim2));
112-
return array(out);
120+
return ifft3(in, norm_factor, odim0, odim1, odim2);
121+
}
122+
123+
array ifft(const array& in, double norm_factor, const dim4 outDims)
124+
{
125+
array temp;
126+
switch(in.dims().ndims()) {
127+
case 1: temp = ifft(in, norm_factor, outDims[0]); break;
128+
case 2: temp = ifft2(in, norm_factor, outDims[0], outDims[1]); break;
129+
case 3: temp = ifft3(in, norm_factor, outDims[0], outDims[1], outDims[2]); break;
130+
default: AF_THROW(AF_ERR_NOT_SUPPORTED);
131+
}
132+
return temp;
133+
}
134+
135+
array ifft(const array& in, const dim4 outDims)
136+
{
137+
return ifft(in, 1.0, outDims);
138+
}
139+
140+
array ifft(const array& in)
141+
{
142+
return ifft(in, 1.0, dim4(0,0,0,0));
113143
}
114144

115145
}

0 commit comments

Comments
 (0)