Exponent of next higher power of 2
P = nextpow2( returns the exponents for the
smallest powers of two that satisfy
for each element in
A. By convention,
nextpow2(0) returns zero.
You can use
nextpow2 to pad the signal you pass to
fft. Doing so can speed up the
computation of the FFT when the signal length is not an exact power of 2.
Next Power of 2 of Double Integer Values
Define a vector of
double integer values and calculate the exponents for the next power of 2 higher than those values.
a = [1 -2 3 -4 5 9 519]; p = nextpow2(a)
p = 1×7 0 1 2 2 3 4 10
Calculate the positive next powers of 2.
np2 = 2.^p
np2 = 1×7 1 2 4 4 8 16 1024
Preserve the sign of the original input values.
ans = 1×7 1 -2 4 -4 8 16 1024
Next Power of 2 of Unsigned Integer Values
Define a vector of unsigned integers and calculate the exponents for the next power of 2 higher than those values.
a = uint32([1020 4000 32700]); p = nextpow2(a)
p = 1x3 uint32 row vector 10 12 15
Calculate the next powers of 2 higher than the values in
ans = 1x3 uint32 row vector 1024 4096 32768
Optimize FFT with Padding
nextpow2 function to increase the performance of
fft when the length of a signal is not a power of 2.
Create a 1-D vector containing 8191 sample values.
rng default; x = rand([1,8191]);
Calculate the next power of 2 higher than 8191.
p = nextpow2(8191); n = 2^p
n = 8192
Pass the signal and the next power of 2 to the
y = fft(x,n);
A — Input values
scalar, vector, array, table, or timetable of real numbers
Input values, specified as a scalar, vector, array, table, or timetable of real numbers of any numeric type.
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version HistoryIntroduced before R2006a
R2023a: Perform calculations directly on tables and timetables
nextpow2 function can calculate on all variables within a table or
timetable without indexing to access those variables. All variables must have data types
that support the calculation. For more information, see Direct Calculations on Tables and Timetables.