Why can't an fft function output an int data type?
12 views (last 30 days)
I am wanting to experiment with model using floating and fixed-point. I understand the fft cannot handle fixed-point data however it can take integer data types but only outputs single and double floating-points. Why can the fft functions not also output integer data types?
If I input an integer array into an fft, then convert the fft output into integer array will there be any internal chnages to the data?
Andy Bartlett on 8 Jun 2021
Edited: Andy Bartlett on 8 Jun 2021
MATLAB's built-in types are generally designed to favor operations producing an output of the same type as the inputs.
(Key exceptions to this are scalar doubles are freely mixed with other types and logical also mixes in some cases.)
In the world of types with luxurious numerics like double and single, there is often no downside to inputs and outputs all using the same type.
But as Walter showed for an fft operation, homogeneous use of the same integer for input and output doesn't fit the need.
When dealing with types that do not have luxuriously big range and precision, mixing different types for inputs, outputs, states, and temporary variables is a time honored approach to achieving good balance of numeric behavior and efficiency.
The fixed-point types in MATLAB and Simulink and the functions that support these types are typically designed to support a mixture of types. Take a look at the FFT provided by DSP System Toolbox as a MATLAB callable system object dsp.FFT or as the FFT block. In their documentation, you'll see that they support fixed-point and provide different types for input, output, productDataType, AccumulatorDataType. The different elements can have different wordlengths and scaling.
The following example shows the dsp.FFT system object handling inputs that are integer or fixed-point.
nPts = 2^13;
halfN = nPts / 2;
Fs = 800;
t = (0:nPts-1)'/Fs;
fv = linspace(0,Fs,halfN);
x = sin(2*pi*250*t) + 0.75*cos(2*pi*340*t) + 0.3*sin(2*pi*200*t + 0.7*pi);
u = x + .3*randn(size(x)); % noisy signal
u = int16( 4000 * u );
% Can convert input to fi before feeding to dsp.FFT
% but don't have to.
%u = fi(u);
ft2 = dsp.FFT;
y2 = ft2(u);
By default, the FFT object automatically selected the output data type.
DataTypeMode: Fixed-point: binary point scaling
Under the hood, dsp.FFT is really designed to handle fixed-point objects, not built-in MATLAB integers. But the object has a friendly design that automatically converts the built-in integer input to its fi equivalent.
If you encounter some code that has issues handling built-in integers, try doing a conversion
a = int16(1234)
b = fi(a)
c = castIntToFi(a)
Either of the two approaches easily converted the built-in integer to its fi equivalent.