-
Notifications
You must be signed in to change notification settings - Fork 3
/
sg.m
44 lines (40 loc) · 1.01 KB
/
sg.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
function [yo,fo,to] = sg(x,nfft,Fs,WINDOW,noverlap)
nx = length(x);
nwind = length(WINDOW);
if nx < nwind % zero-pad x if it has length less than the window length
x(nwind)=0; nx=nwind;
end
x = x(:); % make a column vector for ease later
WINDOW = WINDOW(:); % be consistent with data set
ncol = fix((nx-noverlap)/(nwind-noverlap));
colindex = 1 + (0:(ncol-1))*(nwind-noverlap);
rowindex = (1:nwind)';
if length(x)<(nwind+colindex(ncol)-1)
x(nwind+colindex(ncol)-1) = 0; % zero-pad x
end
y = zeros(nwind,ncol);
y(:) = x(rowindex(:,ones(1,ncol))+colindex(ones(nwind,1),:)-1);
y = WINDOW(:,ones(1,ncol)).*y;
y = fft(y,nfft);
if ~any(any(imag(x))) % x purely real
if rem(nfft,2), % nfft odd
select = [1:(nfft+1)/2];
else
select = [1:nfft/2+1];
end
y = y(select,:);
else
select = 1:nfft;
end
f = (select - 1)'*Fs/nfft;
t = (colindex-1)'/Fs;
if nargout == 1,
yo = y;
elseif nargout == 2,
yo = y;
fo = f;
elseif nargout == 3,
yo = y;
fo = f;
to = t;
end