-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectrum.m
44 lines (37 loc) · 918 Bytes
/
spectrum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [y,f]=spectrum(x,nfft,nw)
%SPECTRUM Power spectral density using Hanning window
% [y,f]=spectrum(x,nfft,nw)
% See also: psd.m in Signal Processing Toolbox
% Marko Laine <Marko.Laine@Helsinki.FI>
% $Revision: 1.3 $ $Date: 2007/08/10 08:54:49 $
if nargin < 2 | isempty(nfft)
nfft = min(length(x),256);
end
if nargin < 3 | isempty(nw)
nw = fix(nfft/4);
end
noverlap = fix(nw/2);
% Hanning window
w = .5*(1 - cos(2*pi*(1:nw)'/(nw+1)));
% Daniel
%w = [0.5;ones(nw-2,1);0.5];
n = length(x);
if n < nw
x(nw)=0; n=nw;
end
x = x(:);
k = fix((n-noverlap)/(nw-noverlap)); % no of windows
index = 1:nw;
kmu = k*norm(w)^2; % Normalizing scale factor
y = zeros(nfft,1);
for i=1:k
% xw = w.*detrend(x(index),'linear');
xw = w.*x(index);
index = index + (nw - noverlap);
Xx = abs(fft(xw,nfft)).^2;
y = y + Xx;
end
y = y*(1/kmu); % normalize
n2 = floor(nfft/2);
y = y(1:n2);
f = 1./n*(0:(n2-1));