7

What is the easiest way to (zero) pad a matlab array?
i.e. given [1,2,3,4] and length 6 return [1,2,3,4,0,0]

Background

I have a data array which I would like to apply a windowing function to before running fft on the data.

I use to pass data directly to fft which would zero pad to the next power of 2, but now I need it zero padding before the fft so I can multiply by the window function.

fs = 100;                          % Sample frequency (Hz)
t = 0:1/fs:10-1/fs;                % 10 sec sample
x = (1.3)*sin(2*pi*15*t) ...       % 15 Hz component
  + (1.7)*sin(2*pi*40*(t-2)) ...   % 40 Hz component
  + (2.5)*randn(size(t));          % Gaussian noise;

m = length(x);          % Window length
n = pow2(nextpow2(m));  % Transform length
w = barthannwin( n );   % FFT Window

y = fft(data, n);       % DFT

windowed_data = x*w ; % Dimensions do not match as x not padded
y = fft(windowed_data, n); % DFT

I am aware of padarray as part of the Image Processing Toolbox, which I do not have.

0

4 Answers 4

9

EDIT

This method is probably even better for vectors as it does not break when they are transposed, note that it will change the original vector which may not be desirable:

myVec = 1:7;
myVec(end+3)=0

Alternately you can just concatenate zeros and the vector that you have and create a new variable with it.

myVec = 1:7;
requiredpadding = 10-7;
myVecPadded=[myVec zeros(1,requiredpadding)]
0
3

There is no built in function to do padding, but here is a little function to pad vector x given a minimum length n.

function y = pad(x, n)
y = x;
if length(x) < n
    y(n) = 0;
end
1

this should pad it with zeros to the nearest power of 2 for an array a:

a(2^ceil(log2(length(a))))=0;

3
  • It appears you forgot to remove the end: so i fixed that for you. Feb 15, 2013 at 18:00
  • Interesting. I didn't realize MATLAB would just fill unspecified array indices with zeros.
    – Doresoom
    Feb 15, 2013 at 19:53
  • @ZZZ - what if the original length of a is a power of 2? I suspect, in that case you would overwrite the last element!
    – Shai
    Feb 18, 2013 at 8:50
0

The image-processing toolbox of Matlab has a built-in function to pad arrays padarray(A,dim,value

For your example:

A = [1, 2, 3, 4];
dimension = [0 1]; % pad with extra columns
size = 2; % how much to pad
B = padarray(A,size*dimension,'post') % 'post' says to pad at the end
% just for demonstration, let's pre-pad the first dimension (rows)
dimension = [1 0];
C = padarray(A,dimension,'pre') % just as an example
% or pad in both directions
dimension = [1 2];
D = padarray(A,dimension) % by default, it will pad both pre and post

returns

B =

 1     2     3     4     0     0

C =

 0     0     0     0
 1     2     3     4
D =

 0     0     0     0     0     0     0     0
 0     0     1     2     3     4     0     0
 0     0     0     0     0     0     0     0

You can also use this for multi-dimensional arrays, the dimension vector just needs to be extended correspondingly, i.e. dimension = [0 0 1] will extend in the 3rd dimension.

2
  • Thanks for the answer, but I believe this part of a toolbox, not built -in.
    – Morgan
    Mar 16, 2014 at 20:18
  • You're right, my bad. It's part of the image-processing toolbox.
    – Steve Heim
    Mar 16, 2014 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.