-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadPSD.m
executable file
·130 lines (117 loc) · 3.23 KB
/
ReadPSD.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
function data = ReadPSD( nrun, antenna, varargin )
% Read PSD files
% VN March 2011
% Adaptated 08/06/2011 OMH
% Change files path 14/07 TS
% Parse inputs
%===
NPAR = 2*ones( size( antenna ) );
if ~isempty( varargin )
if length( varargin{ 1 } ) == 1
if strcmp( varargin{ 1 }, 'old' )
NPAR = 0*ones( size( antenna ) );
end
else
for k = 1:length( antenna )
if strcmp( varargin{ 1 }{ k }, 'old' )
NPAR( k ) = 0;
end
end
end
end
data = [];
dataAllRuns = [];
for k = 1:length( antenna )
for l = 1:length(nrun)
datanew = ReadPSD1( nrun(l), antenna( k ), NPAR( k ) ) ;
% Chain data (if exists) for all runs
if size(datanew,1)>0
if l==1
dataAllRuns = datanew;
else
dataAllRuns.psd = [ dataAllRuns.psd datanew.psd];
dataAllRuns.t = [ dataAllRuns.t datanew.t];
dataAllRuns.ttot = [ dataAllRuns.ttot datanew.ttot];
dataAllRuns.mu = [ dataAllRuns.mu datanew.mu];
dataAllRuns.sigma = [ dataAllRuns.sigma datanew.sigma];
end
end
end
data = [data dataAllRuns];
end
function data = ReadPSD1( nrun, antenna, NPAR )
% Settings
%===
SharedGlobals;
N = NFFT + NPAR;
% Read data
%===
%strfile = [PSD_PATH sprintf('R%06d/R%06d_A%04d_PSD_data.bin',nrun,nrun,antenna)];
strfile = [PSD_PATH sprintf('R%06d_A%04d_PSD_data.bin',nrun,antenna)];
fid = fopen( strfile );
if fid < 0 %Old format
%disp(sprintf( 'Error in READ_PSD\n could not find file %s', strfile ) )
strfile = [PSD_PATH sprintf('PSD%05d/PSD%05d_%d_data.bin', nrun,nrun, antenna )];
fid = fopen( strfile );
end
if fid <= 0
data = [];
disp( sprintf( 'Error in READ_PSD\n could not find file %s', strfile ) );
return
end
d = fread( fid, inf, 'float32' );
fclose( fid );
d = reshape( d, N, [] );
df = 0.5*FSAMPLING/N;
F = [ 1:NFFT ]*df;
P = 10*log10( d( 1:NFFT, : )*SCALE^2/df/0.3743 ); %0.3743 arrises from Hann window [V^2/Hz]dB
if NPAR > 0
mu = d( NFFT+1, : );
sig = d( NFFT+2, : );
else
mu = [];
sig = [];
end
% Read time
%===
%strfile = [PSD_PATH sprintf('R%06d/R%06d_A%04d_PSD_time.bin', nrun, nrun, antenna )];
strfile = [PSD_PATH sprintf('R%06d_A%04d_PSD_time.bin', nrun, antenna )];
fid = fopen( strfile );
if fid < 0 %Old format
strfile = [PSD_PATH sprintf('R%06d/PSD%05d_%d_time.bin', nrun, nrun, antenna )];
fid = fopen( strfile );
end
if fid >= 0
d = fread( fid, [ 4, inf ], 'int32' );
fclose( fid );
Tdeb = min(d(1,:));
T = d( 1, : ) - Tdeb;
flg = d( 3, : ) - d( 2, : );
else
T = [];
flg = [];
Tdeb = 0;
end
% Clean
%===
if length( T ) > 0
nmin = min( [ size( P, 2 ), length( T ) ] );
P = P( :, 1:nmin );
T = T( 1:nmin );
flg = flg( 1:nmin );
if length( mu > 0 )
mu = mu( 1:nmin );
sig = sig( 1:nmin );
end
end
% Pack results
%===
data.id = antenna;
data.psd = P;
data.f = F;
data.t = T;
data.ttot = Tdeb+T; %[s]
data.tdeb = Tdeb;
data.flag = flg;
data.mu = mu;
data.sigma = sig;