-
Notifications
You must be signed in to change notification settings - Fork 1
/
estimate_spectras.m
73 lines (55 loc) · 1.97 KB
/
estimate_spectras.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%
% This file is part of pichim's controller tuning framework.
%
% This sofware is free. You can redistribute this software
% and/or modify this software under the terms of the GNU General
% Public License as published by the Free Software Foundation,
% either version 3 of the License, or (at your option) any later
% version.
%
% This software is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public
% License along with this software.
%
% If not, see <http:%www.gnu.org/licenses/>.
%
%%
function [Pavg, freq] = estimate_spectras(inp, window, Noverlap, Nest, Ts)
% Todo: check if it is usefull to remove mean here
inp = inp - mean(inp);
[Ndata, Nsignals] = size(inp);
% compute the frequency bins for the output x-axis
df = 1 / (Nest * Ts);
freq = (0:df:1/Ts-df).';
ind = freq <= 1 / (2 * Ts);
freq = freq(ind);
Nfreq = length(freq);
% factor 2 so that the magnitude corresponds to a single sided spectrum
% 2.3*sin(2*pi*f0*time) <=> sqrt(puu(f0)) = 2.3
W = sum(window) / Nest / 2;
Pavg = zeros(Nfreq, Nsignals);
for i = 1:Nsignals
Navg = 0;
ind_start = 1;
ind_end = Nest;
Ndelta = Nest - Noverlap;
while ind_end <= Ndata
inp_act = inp(ind_start:ind_end,i);
% Todo: check if it is usefull to remove mean here
inp_act = inp_act - mean(inp_act);
inp_act = window .* inp_act;
U = fft(inp_act) / (Nest * W);
Pact = U .* conj(U);
Pavg(:,i) = Pavg(:,i) + Pact(1:Nfreq);
Navg = Navg + 1;
ind_start = ind_start + Ndelta;
ind_end = ind_end + Ndelta;
end
Pavg(:,i) = Pavg(:,i) / Navg;
end
end