ita_writeunv58.m 13.7 KB
Newer Older
1
2
3
4
5
function ita_writeunv58(varargin)
%ITA_WRITEUNV58 - write data as frequency response per mesh node as a unv58 dataset
%  This function takes a struct of measurement data as input and writes the
%  frequency response for each node of a mesh into the specified file.
%
6
7
%  An optional third argument 'action', value 'add' or 'replace', controls
%  whether the data is appended to a file (if it already exists).
8
%  By default, the file contents are replaced.
9
%
10
11
12
13
14
%  Call: ita_writeunv58(Data,unvFilename)
%  Call: ita_unvwrite58(Data,unvFilename,'action','add')
%
%   See also ita_fft, ita_ifft, ita_ita_read, ita_ita_write, ita_metainfo_rm_channelsettings, ita_metainfo_add_picture, ita_impedance_parallel, ita_unv2unv, ita_readunv58, ita_readunv2414.
%
15
%   Reference page in Help browser
16
17
18
%        <a href="matlab:doc ita_writeunv58">doc ita_writeunv58</a>

% <ITA-Toolbox>
19
20
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
21
22
23
24
% </ITA-Toolbox>


% Author: MMT -- Email: mmt@akustik.rwth-aachen.de
25
% Created: 23-Nov-2008
26
27
28
29
30

%% Initialization
% Number of Input Arguments
narginchk(2,4);
sArgs        = struct('pos1_Data','itaSuper','pos2_unvFilename','string','action','replace');
31
[Data,unvFilename,sArgs] = ita_parse_arguments(sArgs,varargin);
32

33
%% Gather meta data
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
ita_verbose_info('ITA_WRITEUNV58::preparing ...',2);
if isa(Data.channelCoordinates,'itaMeshNodes') && ~any(isnan(Data.channelCoordinates.ID))
    nodes = Data.channelCoordinates.ID;
else
    uData = Data.userData;
    if ~isempty(find(strcmpi(uData,'nodeN')==1,1))
        nodes = uData{find(strcmpi(uData,'nodeN')==1)+1}(:); % node IDs
    else
        error([upper(mfilename) ':could not find node IDs']);
    end
end

if ispc
    zeroString = '0.0000e+000';
    format      = '%1.4e';
else
    zeroString = '0.00000e+00';
    format      = '%1.5e';
end

dateStr = ['Date (DD/MM/YYYY)      ' datestr(now,24)];
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

%% gather actual data

switch Data.domain
    case 'time'
        X = Data.timeVector;
        Y = Data.timeData;
    case 'freq'
        X = Data.freqVector;
        Y = Data.freqData;
    otherwise
        error('Input has a non-supported domain')
end

nodes     = nodes(:);
[nodes,sortIds] = sort(nodes);
Y         = Y(:,sortIds);

nNodes    = numel(nodes);
74
nNodesStr = num2str(nNodes);
75
76
77
78
nX        = size(X,1);
nXStr     = num2str(nX);
spacer    = ' ';
spacerX   = spacer(:,ones(1,max(2,10-size(nXStr,2))));
79
80

% this is really stupid but necessary
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
line1    = ['    -1' spacer(:,ones(1,80-6))]; % delimiter
line2    = ['    58' spacer(:,ones(1,80-4))]; % dataset
line3    = ['ita_writeunv58' spacer(:,ones(1,80-11))]; % ID
line4    = ['NONE' spacer(:,ones(1,80-4))]; % ID
line5    = [dateStr spacer(:,ones(1,80-33))]; % date
line7    = ['Trust no one.' spacer(:,ones(1,80-13))]; % ID

switch Data.domain
    case 'time'
        if isa(Data,'itaAudio')
            dataDivider = 6;
            line9str = ['         2' spacerX nXStr '         1  ' zeroString '  ' num2str(1/Data.samplingRate,format) '  ' zeroString];
        else
            dataDivider = 3;
            line9str = ['         2' spacerX nXStr '         0  ' zeroString '  ' zeroString '  ' zeroString];
        end
        line10   = ['        17    0    0    0 NONE                  s' spacer(:,ones(1,80-49))];
    case 'freq'
        dataDivider = 2;
        
        line9str = ['         5' spacerX nXStr '         0  ' zeroString '  ' zeroString '  ' zeroString];
        line10   = ['        18    0    0    0 NONE                 Hz' spacer(:,ones(1,80-49))];
end
xNum      = floor(nX/dataDivider);
xLeft     = mod(nX,dataDivider);

line9    = [line9str spacer(:,ones(1,max(0,80-length(line9str))))];
line11 = ['         1    0    0    0 NONE                 ' Data.channelUnits{1} spacer(:,ones(1,80-50))];
line12 = ['         0    0    0    0 NONE                 NONE' spacer(:,ones(1,80-51))];
line13 = ['         0    0    0    0 NONE                 NONE' spacer(:,ones(1,80-51))];
111
112
113
114
115
116
117
118

if strcmp(sArgs.action,'add')
    fid = fopen(unvFilename,'at');
else
    fid = fopen(unvFilename,'wt');
end
% based on free memory use the fast version or the one that has less memory usage
if fid ~= -1
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    if numel(Y) < 0.5*1024*1024
        if strcmpi(Data.domain,'time')
            timeValStr = num2str(Y(:),format);
            nodesStr   = num2str(nodes);
            xStr       = num2str(X,format);
            iStr       = num2str((1:nNodes).');
            spNod      = spacer(:,ones(1,max(2,16-size(nodesStr,2))));
            line6sp = spacer(:,ones(1,max(0,80-11-size(nodesStr,2)))); % separator
            line8sp = spacer(:,ones(1,max(0,80-35-length(spNod)-size(nodesStr,2)-29))); % separator
            for i = 1:nNodes
                ita_verbose_info(['ITA_WRITEUNV58::writing ' nXStr ' samples for node: ' nodesStr(i,:) ' (' iStr(i,:) ' of ' nNodesStr ')'],2);
                fprintf(fid,'%s\n',line1);
                fprintf(fid,'%s\n',line2);
                fprintf(fid,'%s\n',line3);
                fprintf(fid,'%s\n',line4);
                fprintf(fid,'%s\n',line5);
                fprintf(fid,'%s\n',['node ' nodesStr(i,:) line6sp]); % ID
                fprintf(fid,'%s\n',line7);
                fprintf(fid,'%s\n',['    1         0    1         0 NONE' spNod nodesStr(i,:) '   3 NONE               0   3' line8sp]);
                fprintf(fid,'%s\n',line9);
                fprintf(fid,'%s\n',line10);
                fprintf(fid,'%s\n',line11);
                fprintf(fid,'%s\n',line12);
                fprintf(fid,'%s\n',line13);
                for k = 1:xNum
                    switch dataDivider
                        case 6
                            fprintf(fid,'%s\n',['  ' timeValStr(dataDivider*k-5+(i-1)*nX,:) ' ' timeValStr(dataDivider*k-4+(i-1)*nX,:) ' ' timeValStr(dataDivider*k-3+(i-1)*nX,:) ' ' timeValStr(dataDivider*k-2+(i-1)*nX,:) ' ' timeValStr(dataDivider*k-1+(i-1)*nX,:) ' ' timeValStr(dataDivider*k+(i-1)*nX,:)]);
                        case 3
                            fprintf(fid,'%s\n',['  ' xStr(dataDivider*k-2,:) ' ' timeValStr(dataDivider*k-2+(i-1)*nX,:) '  ' xStr(dataDivider*k-1,:) ' ' timeValStr(dataDivider*k-1+(i-1)*nX,:) '  ' xStr(dataDivider*k,:) ' ' timeValStr(dataDivider*k+(i-1)*nX,:)]);
                    end
                end
                if xLeft
                    switch dataDivider
                        case 6
                            oddStr = ['  ' timeValStr(dataDivider*xNum + 1 + (i-1)*nX,:)];
                            for iLeft = 2:xLeft
                                oddStr = [oddStr ' ' timeValStr(dataDivider*xNum + iLeft + (i-1)*nX,:)]; %#ok<AGROW>
                            end
                        case 3
                            oddStr = ['  ' xStr(dataDivider*xNum + 1,:) ' ' timeValStr(dataDivider*xNum + 1 + (i-1)*nX,:)];
                            for iLeft = 2:xLeft
                                oddStr = [oddStr ' ' xStr(dataDivider*xNum + iLeft,:) ' ' timeValStr(dataDivider*xNum + iLeft + (i-1)*nX,:)]; %#ok<AGROW>
                            end
                    end
                    fprintf(fid,'%s\n',[oddStr spacer(:,ones(1,max(0,80-length(oddStr))))]);
                end
                fprintf(fid,'%s\n',line1);
167
            end
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        else
            realStr  = num2str(real(Y(:)),format);
            imagStr  = num2str(imag(Y(:)),format);
            nodesStr = num2str(nodes);
            xStr     = num2str(X,format);
            iStr     = num2str((1:nNodes).');
            spNod    = spacer(:,ones(1,max(2,16-size(nodesStr,2))));
            line6sp  = spacer(:,ones(1,max(0,80-11-size(nodesStr,2)))); % separator
            line8sp  = spacer(:,ones(1,max(0,80-35-length(spNod)-size(nodesStr,2)-29))); % separator
            for i = 1:nNodes
                ita_verbose_info(['ITA_WRITEUNV58::writing ' nXStr ' frequency bins for node: ' nodesStr(i,:) ' (' iStr(i,:) ' of ' nNodesStr ')'],2);
                fprintf(fid,'%s\n',line1);
                fprintf(fid,'%s\n',line2);
                fprintf(fid,'%s\n',line3);
                fprintf(fid,'%s\n',line4);
                fprintf(fid,'%s\n',line5);
                fprintf(fid,'%s\n',['node ' nodesStr(i,:) line6sp]); % ID
                fprintf(fid,'%s\n',line7);
                fprintf(fid,'%s\n',['    1         0    1         0 NONE' spNod nodesStr(i,:) '   3 NONE               0   3' line8sp]);
                fprintf(fid,'%s\n',line9);
                fprintf(fid,'%s\n',line10);
                fprintf(fid,'%s\n',line11);
                fprintf(fid,'%s\n',line12);
                fprintf(fid,'%s\n',line13);
                for k = 1:xNum
                    fprintf(fid,'%s\n',['  ' xStr(2*k-1,:) ' ' realStr(2*k-1+(i-1)*nX,:) ' ' imagStr(2*k-1+(i-1)*nX,:) '  ' xStr(2*k,:) ' ' realStr(2*k+(i-1)*nX,:) ' ' imagStr(2*k+(i-1)*nX,:) '  ']);
                end
                if xLeft
                    oddStr = ['  ' xStr(end,:) ' ' realStr(i*nX,:) ' ' imagStr(i*nX,:)];
                    fprintf(fid,'%s\n',[oddStr spacer(:,ones(1,max(0,80-length(oddStr))))]);
                end
                fprintf(fid,'%s\n',line1);
200
201
202
203
            end
        end
    else
        for i = 1:nNodes
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            if strcmpi(Data.domain,'time')
                ita_verbose_info(['ITA_WRITEUNV58::writing ' nXStr ' samples bins for node: ' num2str(nodes(i)) ' (' num2str(i) ' of ' nNodesStr ')'],2);
                fprintf(fid,'%s\n',line1);
                fprintf(fid,'%s\n',line2);
                fprintf(fid,'%s\n',line3);
                fprintf(fid,'%s\n',line4);
                fprintf(fid,'%s\n',line5);
                fprintf(fid,'%s\n',['node ' num2str(nodes(i)) spacer(:,ones(1,max(0,80-11-size(num2str(nodes(i)),2))))]); % ID
                fprintf(fid,'%s\n',line7);
                fprintf(fid,'%s\n',['    1         0    1         0 NONE' spacer(:,ones(1,max(2,16-size(num2str(nodes(i)),2)))) num2str(nodes(i)) '   3 NONE               0   3' spacer(:,ones(1,max(0,80-35-length(spacer(:,ones(1,max(2,16-size(num2str(nodes(i)),2)))))-size(num2str(nodes(i)),2)-29)))]);
                fprintf(fid,'%s\n',line9);
                fprintf(fid,'%s\n',line10);
                fprintf(fid,'%s\n',line11);
                fprintf(fid,'%s\n',line12);
                fprintf(fid,'%s\n',line13);
                for k = 1:xNum
                    switch dataDivider
                        case 6
                            fprintf(fid,'%s\n',['  ' num2str(Y(dataDivider*k-5+(i-1)*nX),format) ' ' num2str(Y(dataDivider*k-4+(i-1)*nX),format) ' ' num2str(Y(dataDivider*k-3+(i-1)*nX),format) ' ' num2str(Y(dataDivider*k-2+(i-1)*nX),format) ' ' num2str(Y(dataDivider*k-1+(i-1)*nX),format) ' ' num2str(Y(dataDivider*k+(i-1)*nX),format)]);
                        case 3
                            fprintf(fid,'%s\n',['  ' num2str(X,(dataDivider*k-2),format) ' ' num2str(Y(dataDivider*k-2+(i-1)*nX),format) '  ' num2str(X(dataDivider*k-1),format) ' ' num2str(Y(dataDivider*k-1+(i-1)*nX),format) '  ' num2str(X(dataDivider*k),format) ' ' num2str(Y(dataDivider*k+(i-1)*nX),format)]);
                    end
                end
                if xLeft
                    switch dataDivider
                        case 6
                            oddStr = ['  ' num2str(Y(dataDivider*xNum + 1 + (i-1)*nX),format)];
                            for iLeft = 2:xLeft
                                oddStr = [oddStr ' ' num2str(Y(dataDivider*xNum + iLeft + (i-1)*nX),format)]; %#ok<AGROW>
                            end
                        case 3
                            oddStr = ['  ' num2str(X(dataDivider*xNum + 1),format) ' ' num2str(Y(dataDivider*xNum + 1 + (i-1)*nX),format)];
                            for iLeft = 2:xLeft
                                oddStr = [oddStr ' ' num2str(X(dataDivider*xNum + iLeft),format) ' ' num2str(Y(dataDivider*xNum + iLeft + (i-1)*nX),format)]; %#ok<AGROW>
                            end
                    end
                    fprintf(fid,'%s\n',[oddStr spacer(:,ones(1,max(0,80-length(oddStr))))]);
                end
                fprintf(fid,'%s\n',line1);
            else
                ita_verbose_info(['ITA_WRITEUNV58::writing ' nXStr ' frequency bins for node: ' num2str(nodes(i)) ' (' num2str(i) ' of ' nNodesStr ')'],2);
                fprintf(fid,'%s\n',line1);
                fprintf(fid,'%s\n',line2);
                fprintf(fid,'%s\n',line3);
                fprintf(fid,'%s\n',line4);
                fprintf(fid,'%s\n',line5);
                fprintf(fid,'%s\n',['node ' num2str(nodes(i)) spacer(:,ones(1,max(0,80-11-size(num2str(nodes(i)),2))))]); % ID
                fprintf(fid,'%s\n',line7);
                fprintf(fid,'%s\n',['   12         0    1         0 NONE' spacer(:,ones(1,max(2,16-size(num2str(nodes(i)),2)))) num2str(nodes(i)) '   3 NONE               0   3' spacer(:,ones(1,max(0,80-35-length(spacer(:,ones(1,max(2,16-size(num2str(nodes(i)),2)))))-size(num2str(nodes(i)),2)-29)))]);
                fprintf(fid,'%s\n',line9);
                fprintf(fid,'%s\n',line10);
                fprintf(fid,'%s\n',line11);
                fprintf(fid,'%s\n',line12);
                fprintf(fid,'%s\n',line13);
                for k = 1:xNum
                    fprintf(fid,'%s\n',['  ' num2str(X(2*k-1),format) ' ' num2str(real(Y(2*k-1+(i-1)*nX)),format) ' ' num2str(imag(Y(2*k-1+(i-1)*nX)),format) '  ' num2str(X(2*k),format) ' ' num2str(real(Y(2*k+(i-1)*nX)),format) ' ' num2str(imag(Y(2*k+(i-1)*nX)),format) '  ']);
                end
                if xLeft
                    oddStr = ['  ' num2str(X(end),format) ' ' num2str(real(Y(i*nX)),format) ' ' num2str(imag(Y(i*nX)),format)];
                    fprintf(fid,'%s\n',[oddStr spacer(:,ones(1,max(0,80-length(oddStr))))]);
                end
                fprintf(fid,'%s\n',line1);
266
267
268
269
270
271
272
273
274
275
            end
        end
    end
    fclose(fid);
else
    error('ITA_WRITEUNV58::cannot create file');
end

%end function
end