MATLAB Answers

Why can't an fft function output an int data type?

조회 수: 5(최근 30일)
Joshua Ford
Joshua Ford 2021년 5월 12일
편집: Andy Bartlett 2021년 6월 8일
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?
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 5월 14일
Reminder that the first output bin is the sum of the signal. For unsigned integer, for sufficiently long signal that is non-zero, that is going to overflow the integer data type. For signed integer it will not necessarily overflow, but it could easily.

댓글을 달려면 로그인하십시오.

답변(1개)

Andy Bartlett
Andy Bartlett 2021년 6월 8일
편집: Andy Bartlett 2021년 6월 8일
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);
plot(fv,abs(y2(1:halfN)))
shg
By default, the FFT object automatically selected the output data type.
>> numerictype(y2)
ans =
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 29
FractionLength: 0
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.
a =
int16
1234
b =
1234
numerictype(1,16,0)
c =
1234
numerictype(1,16,0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by