Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# nextpow2

Exponent of next higher power of 2

## Description

example

P = nextpow2(A) returns the exponents for the smallest powers of two that satisfy

for each element in A.

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.

## Examples

expand all

### 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 =

0     1     2     2     3     4    10
```

Calculate the positive next powers of 2.

`np2 = 2.^p`
```np2 =

1     2     4     4     8     16    1024
```

Preserve the sign of the original input values.

`np2.*sign(a)`
```ans =

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 =

10       12       15
```

Calculate the next powers of 2 higher than the values in a.

`2.^p`
```ans =

1024      4096     32768```

### Optimize FFT with Padding

Use nextpow2 to increase the performance of fft when the length of your signal is not a power of 2.

Create a 1-D vector containing 8191 sample values.

```x = gallery('uniformdata',[1,8191],0);
```

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 fft function.

```y = fft(x,n);
```

## Input Arguments

expand all

### A — Input valuesscalar, vector, or array of real numbers

Input values, specified as a scalar, vector, or array of real numbers of any numeric type.

Example: 15

Example: [-15.123 32.456 63.111]

Example: int16([-15 32 63])

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

## See Also

Was this topic helpful?