# MATLAB Hamming window

I have been trying to achieve a Hamming window length of 23 using the equation.

I tried several coding, one of the is the one below:

M=23
w = .54 + .46*cos(pi*(-M:M-1)'/M)
plot(w)


If the length of the window should be $23$, $M$ must equal $(23-1)/2=11$:

M = 11;
m = -M:M;
w = .54 + .46*cos(pi*m/M);
plot(m,w)


You need to do two things:

• Evaluate the cosine only at 23 points, not at 46 like you're doing in your code above.
• Generate a time vector to go with your window signal. This means you'll also need to specify your sampling frequency.

You can do it like this:

M = 23;    % samples in the window
Ts = 1;    % sampling interval
w = 0.54+0.46*cos((2*pi/(M-1))*(-(M-1)/2:(M-1)/2));  % window
t = Ts*(-(M-1)/2:(M-1)/2);  % time vector
plot(t,w);


Note that I've used a slightly different definition of the window formula. This code produces the same window as Matlab's hamming command.

There is a default function in MatLab which can generate this for you, called hamming. In case you do not have access to this function, because you do not have the signal processing toolbox, you could try:

M = 23;
w = 0.54 - 0.46 * cos(2 * pi * linspace(0, 1, M));