forked from NYU-ImmersiveAudio/ScanIR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanIR_v2.m
1885 lines (1625 loc) · 71.2 KB
/
ScanIR_v2.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
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
167
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
200
201
202
203
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = ScanIR_v2(varargin)
%SCANIR_V2 MATLAB code file for ScanIR_v2.fig
% SCANIR_V2, by itself, creates a new SCANIR_V2 or raises the existing
% singleton*.
%
% H = SCANIR_V2 returns the handle to a new SCANIR_V2 or the handle to
% the existing singleton*.
%
% SCANIR_V2('Property','Value',...) creates a new SCANIR_V2 using the
% given property value pairs. Unrecognized properties are passed via
% varargin to ScanIR_v2_export_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% SCANIR_V2('CALLBACK') and SCANIR_V2('CALLBACK',hObject,...) call the
% local function named CALLBACK in SCANIR_V2.M with the given input
% arguments.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ScanIR_v2
% Version 2 Written by Andrea Genovese and Julian Vanasse
% NYU Music and Audio Research Lab
% Copyright 2019
%
% Version 1 Written by Agnieszka Roginska and Braxton Boren
% NYU Music and Audio Research Lab
% Copyright 2011
% Last Modified by GUIDE v2.5 05-Oct-2019 22:01:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ScanIR_v2_OpeningFcn, ...
'gui_OutputFcn', @ScanIR_v2_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ScanIR_v2 is made visible.
function ScanIR_v2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ScanIR_v2 (see VARARGIN)
% Choose default command line output for ScanIR_v2
handles.output = hObject;
% open program source directories
addpath(genpath('src'))
addpath('api/');
addpath('WavFiles/');
disp(['ScanIR is a measurement tool developed by NYU Steinhardt`s Music',...
' and Audio Research Lab.']);
% add toolbar for zooming axes
set(hObject,'toolbar','figure');
handles.data = [];
handles.specs = [];
handles.app = [];
handles.specs.filtertype = 'fixed filters';
handles.specs.subjectName = [];
handles.specs.database = [];
handles.specs.comments = [];
handles.app.currID = 1;
handles.app.npositions = []; % number of total recording positions for HRIRs for each series of recordings
handles.app.sorted = 0; % 0 if unsorted, 1 if sorted
handles.app.azPositionData = [str2double(get(handles.az_start,'String')) str2double(get(handles.az_interval, 'String')) str2double(get(handles.az_end, 'String')) ];
handles.app.elPositionData = [str2double(get(handles.el_start,'String')) str2double(get(handles.el_interval, 'String')) str2double(get(handles.el_end, 'String')) ];
handles.app.seriesInfo = []; % stores the ID numbers of positions at the end of each series
handles.app.isBRIR = false; % distinguish HRIR from BRIR
handles.app.sig_level = 0.6;
handles.hrir_az = []; % row vector of azimuths for HRIR measurements
handles.hrir_el = []; % row vector of elevations for HRIR measurements
handles.sizeLoadedData = 0;
handles.numSeries = 1; % index of current series
% Step-motor pre-sets - change if needed
handles.motor.SPR = 200;
handles.motor.default_motorport = 2;
handles.motor.deltastep = 360/handles.motor.SPR;
handles.motor.def_pause = 1.0;
handles.motor.nsteps = 1;
handles.motor.stepsize = handles.motor.deltastep;
handles.motor.direction = -1;
handles.motor.inProgress = 0;
set(handles.stepsize_edit,'String',num2str(handles.motor.deltastep));
% Check that the appropriate toolboxes exist
handles = checkPackages(handles);
% Opening GUI
opening = welcome;
disp(opening);
if ( strcmp(opening, 'createNew') )
ard = handles.ard_connected;
save('src/setup/tempfile.mat','ard');
setup = createNew;
handles.app.OS = setup.system;
handles.app.inMode = setup.inMode;
handles.specs.signalType = setup.signalType;
handles.app.sigLength = setup.sigLength;
handles.app.irLength = setup.irLength;
handles.specs.sampleRate = setup.sampleRate;
handles.app.numInputChls = 1:setup.numInputChls;
handles.app.numOutputChls = setup.numOutputChls;
handles.app.outMode = setup.outMode;
handles.app.numPlays = setup.numPlays;
% motor settings
if handles.ard_connected
handles.motor.SPR = setup.motor.spr;
handles.motor.default_motorport = setup.motor.port;
handles.motor.shield = addon(handles.motor.arduino, 'Adafruit\MotorShieldV2');
handles.motor.stepper = stepper(handles.motor.shield,handles.motor.default_motorport,handles.motor.SPR,'StepType','Double');
handles.motor.stepper.RPM = setup.motor.rpm;
end
% audio device selection
handles.specs.IpDeviceInfo = setup.IpDevInfo;
handles.specs.OpDeviceInfo = setup.OpDevInfo;
handles.maxOuts = handles.specs.OpDeviceInfo.NrOutputChannels;
handles.maxIns = handles.specs.IpDeviceInfo.NrInputChannels;
handles.sessionName = setup.sessionName;
set(handles.figure1, 'Name', ['ScanIR Session: ', handles.sessionName]);
set(handles.sessionText,'String',handles.sessionName);
set(handles.textAudioInputDevice,'String',setup.IpDevInfo.DeviceName);
set(handles.textAudioOutputDevice,'String',setup.OpDevInfo.DeviceName);
if (handles.app.sigLength == 1)
set(handles.sigLengthDisp, 'String', strcat(num2str(handles.app.sigLength),' second'));
else
set(handles.sigLengthDisp, 'String', strcat(num2str(handles.app.sigLength),' seconds'));
end
set(handles.irLengthDisp, 'String', strcat(num2str(handles.app.irLength),' samples'));
if (handles.app.inMode == 2)
set(handles.hrir_panel, 'Visible', 'on');
end
elseif ( strcmp(opening, 'loadSession') )
handles = loadFile(hObject, handles);
% find maximum number of output and input channels
InitializePsychSound;
dev = PsychPortAudio('GetDevices');
[m n] = size(dev);
handles.maxOuts = 0;
handles.maxIns = 0;
for k = 1:n
testOuts = getfield(dev, {1,k}, 'NrOutputChannels');
if (handles.maxOuts < testOuts)
handles.maxOuts = testOuts;
end
testIns = getfield(dev, {1,k}, 'NrInputChannels');
if (handles.maxIns < testIns)
handles.maxIns = testIns;
end
end
end
if exist('handles.loaded')
if ( strcmp(handles.loaded, 'fail') )
error('User cancelled file load');
return
end
end
handles.app.outchl = 1:str2double(get(handles.outChannelEdit, 'String'));
% update 'setup' panel
set(handles.sigTypeDisp, 'String', handles.specs.signalType);
set(handles.srateDisp, 'String', strcat(num2str(handles.specs.sampleRate),' Hz'));
set(handles.numinchlsDisp, 'String', num2str(length(handles.app.numInputChls)));
% BRIR flag
if (handles.app.inMode == 2 && handles.app.irLength/handles.specs.sampleRate >= 1)
handles.app.isBRIR = true;
end
% Setup GUI
if (handles.app.inMode == 1) % Mono IR
set(handles.modeDisp, 'String', 'Mono IR ');
set(handles.text21, 'Visible', 'off');
set(handles.chl_popup, 'Visible', 'off');
elseif (handles.app.inMode == 2) % HRIR or BRIR
set(handles.modeDisp, 'String', 'HRIR ');
if handles.app.isBRIR
set(handles.modeDisp, 'String', 'BRIR ');
end
set(handles.az_edit, 'Enable', 'off');
set(handles.el_edit, 'Enable', 'off');
if (handles.app.outMode == 2)
handles.app.npositions(1) = handles.app.numOutputChls;
handles.app.seriesInfo(1) = handles.app.numOutputChls;
set(handles.outChannelEdit, 'Enable', 'off');
set(handles.outChannelEdit, 'String', strcat('1-',num2str(handles.app.npositions(handles.numSeries))));
set(handles.az_end, 'Enable', 'off');
set(handles.el_end, 'Enable', 'off');
end
handles = updateHRIR(hObject,handles);
set(handles.hrir_panel, 'Title', strcat('HRIR Locations for 1-',num2str(handles.app.npositions(handles.numSeries))));
if handles.app.isBRIR
set(handles.hrir_panel, 'Title', strcat('BRIR Locations for 1-',num2str(handles.app.npositions(handles.numSeries))));
end
elseif (handles.app.inMode == 3) % Multichannel IR
set(handles.modeDisp, 'String', 'Multichannel IR');
end
% GUI Plotting pre-sets
if handles.app.inMode > 1
set(handles.plottype_popup, 'String', 'Time-overlay|Frequency|Energy Decay Curve|Time-cascade|Frequency-cascade');
set(handles.plottype_popup, 'Value', 4); % default to Time-cascade
else
set(handles.plottype_popup, 'String', 'Time|Frequency|Energy Decay Curve');
end
for ch = 1:length(handles.app.numInputChls)
channelLabels{ch} = strcat('|Chl: ',num2str(ch));
end
set(handles.chl_popup, 'String', ['All Channels',[channelLabels{:}]]);
handles.fftlen_popup.Enable = 'off';
set(handles.backButton, 'Enable', 'off');
if (size(handles.data,2) < 1)
set(handles.forwardButton, 'Enable', 'off');
end
set(handles.smooth_popup,'String','No Smoothing|1/12 Octave|1/8 Octave|1/4 Octave|1/2 Octave|Full Octave');
set(handles.smooth_popup,'Value',1);
handles.smooth_popup.Enable = 'off';
handles.colors = get(handles.plotarea,'colororder');
% Arduino Initialization
if ~handles.ard_installed
set(handles.arduino_text,'String','not installed');
else
if ~handles.ard_connected
set(handles.arduino_text,'String','not detected');
else
set(handles.arduino_text,'String','connected','FontWeight','bold','ForegroundColor',[0,153/256,0]);
end
end
% GUI Dyanimc Resize Params
ref_ss(1) = (1100/72);
ref_ss(2) = (800/72);
ScreenUnits=get(0,'Units');
set(0,'Units','pixels');
p_ss=get(0,'ScreenSize');
set(0,'Units','inches');
i_ss=get(0,'ScreenSize');
res_ss = p_ss./i_ss;
set(0,'Units',ScreenUnits);
% Set GUI Dimensions
OldUnits = get(hObject, 'Units');
set(hObject, 'Units', 'pixels');
FigPos = get(hObject,'Position');
Width = max([round(ref_ss(1)*res_ss(3)),round(p_ss(3)*0.4)]);
Height = max([round(ref_ss(2)*res_ss(4)),round(p_ss(4)*0.5)]);
if (Width > p_ss(3)-50) Width = p_ss(3)-50; end
if (Height > p_ss(4)-100) Height = p_ss(4)-100; end
FigPos(3)= Width;
FigPos(4)= Height;
FigPos(1)= round((p_ss(3)-FigPos(3))/2);
FigPos(2)= round((p_ss(4)-FigPos(4))/2);
set(hObject, 'Position', FigPos);
set(hObject, 'Units', OldUnits);
% display MARL logo in axes_logo
axes(handles.plotLogo)
[img, map, alphachannel] = imread('MARL_logo.png');
image(img, 'AlphaData', alphachannel);
axis off
axis image
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = ScanIR_v2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in backButton.
function backButton_Callback(hObject, eventdata, handles)
% hObject handle to backButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = goBackward(hObject, handles);
% --- Executes on selection change in fftlen_popup.
function fftlen_popup_Callback(hObject, eventdata, handles)
if (handles.app.currID<=size(handles.data,2))
plotresponse(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function fftlen_popup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function handles = measureIR(hObject, handles)
set(handles.measureButton, 'Enable', 'off');
if (handles.app.currID ~= 1)
set(handles.backButton, 'Enable', 'off');
end
if (handles.app.currID <= size(handles.data,2))
set(handles.forwardButton, 'Enable', 'off');
end
drawnow;
if ( handles.app.outMode == 1 )
handles = measurePosition(hObject, handles);
elseif(handles.app.outMode == 2 && handles.app.currID > size(handles.data,2) )
for k = 1:handles.app.npositions(handles.numSeries)
handles.app.outchl = 1:k;
handles = measurePosition(hObject, handles);
drawnow;
if (k < handles.app.npositions(handles.numSeries))
handles = goForward(hObject, handles);
end
end
elseif ( handles.app.outMode == 2 && handles.app.currID <= size(handles.data,2) )
handles = calcModID(hObject, handles);
handles.app.outchl = 1:handles.modID;
handles = measurePosition(hObject, handles);
end
set(handles.measureButton, 'Enable', 'on');
if (handles.app.currID ~= 1)
set(handles.backButton, 'Enable', 'on');
end
if (handles.app.currID <= size(handles.data,2))
set(handles.forwardButton, 'Enable', 'on');
end
% auto-save data after measurement%
if (get(handles.sort_checkbox, 'Value'))
% sort data before saving
len = size(handles.data,2); % overall number of measured positions
pts = [];
for i = 1:len
pts = [pts; [handles.data(i).elevation handles.data(i).azimuth] ];
end
pts = sortrows(pts, [1 2]); % sort by elevation, then by azimuth when tied
out = [];
for j = 1:len % index over raw data
for k = 1:len % index over sorted data file
if ( (handles.data(j).elevation == pts(k,1)) && (handles.data(j).azimuth == pts(k,2)) )
out(k).azimuth = handles.data(j).azimuth;
out(k).elevation = handles.data(j).elevation;
out(k).distance = handles.data(j).distance;
out(k).IR = handles.data(j).IR;
out(k).ITD = handles.data(j).ITD;
out(k).comments = handles.data(j).comments;
end
end
end
tempData = out; % save sorted data
handles.app.sorted = 1;
else
tempData = handles.data; % save unsorted data
handles.app.sorted = 0;
end
specs = handles.specs; % save entire specs struct
app = handles.app; % save entire ScanIR application-specific struct
save('ScanIR_AutoSave', 'tempData', 'specs', 'app');
guidata(hObject, handles); %updates the handles
% --- Executes on button press in measureButton.
function measureButton_Callback(hObject, eventdata, handles)
handles = measureIR(hObject,handles);
guidata(hObject, handles); %updates the handles
% --- Executes on button press in forwardButton.
function forwardButton_Callback(hObject, eventdata, handles)
handles = goForward(hObject, handles);
% --- Executes on button press in saveButton.
function saveButton_Callback(hObject, eventdata, handles)
if (get(handles.sort_checkbox, 'Value'))
% sort data before saving
len = size(handles.data,2); % overall number of measured positions
pts = [];
for i = 1:len
pts = [pts; [handles.data(i).elevation handles.data(i).azimuth] ];
end
pts = sortrows(pts, [1 2]); % sort by elevation, then by azimuth when tied
out = [];
for j = 1:len % index over raw data
for k = 1:len % index over sorted data file
if ( (handles.data(j).elevation == pts(k,1)) && (handles.data(j).azimuth == pts(k,2)) )
out(k).azimuth = handles.data(j).azimuth;
out(k).elevation = handles.data(j).elevation;
out(k).distance = handles.data(j).distance;
out(k).IR = handles.data(j).IR;
out(k).rawIR = handles.data(k).rawIR;
out(k).ITD = handles.data(j).ITD;
out(k).comments = handles.data(j).comments;
end
end
end
data = out; % save sorted data
handles.app.sorted = 1;
else
data = handles.data; % save unsorted data
handles.app.sorted = 0;
end
% Save file size if raw not desired
if ~(get(handles.raw_checkbox,'Value'))
data(handles.app.currID).rawIR = [];
end
[filename,pathname] = uiputfile(strcat(handles.sessionName, '.mat'), 'Save...');
specs = handles.specs; % save entire specs struct
app = handles.app; % save entire ScanIR application-specific struct
% Save a first time and avoid losing data in case of SOFA API crashing
save(strcat(pathname, filename), 'data', 'specs', 'app');
% --- SOFA save
if get(handles.checkbox_SaveSOFA, 'Value')
try
SOFAstart;
scanIR2sofa(handles.app, handles.data, handles.specs, ...
strcat(pathname, filename(1:end-4), '.sofa'), ...
get(handles.checkbox_isAdvancedSOFA, 'Value'));
catch
warning(['ATTENTION: SOFA saving feature failed!', sprintf('\n'), ...
'You need to download the SOFA API for MATLAB/Octave from:', ...
'<a href="https://www.sofaconventions.org/mediawiki/index.php/Software_and_APIs">SOFA website</a>. ', sprintf('\n'), ...
'Please add the directory to the ScanIR_v2 parent folder to use the SOFA functions']);
end
end
delete 'ScanIR_AutoSave.mat';
function az_edit_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function az_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function el_edit_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function el_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function dist_edit_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function dist_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in plotDomainListBox.
function plotDomainListBox_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function plotDomainListBox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in loadButton.
function loadButton_Callback(hObject, eventdata, handles)
handles = loadFile(hObject, handles);
% function for loading files
function handles = loadFile(hObject, handles)
[filename,pathname] = uigetfile('*.mat;*.sofa', 'Load Session');
if contains(filename, '.mat');
load(strcat(pathname,filename));
handles.loaded = 'succeed';
elseif contains(filename, '.sofa');
[app, data, specs] = sofa2scanIR(strcat(pathname, filename));
handles.loaded = 'succeed';
else
handles.loaded = 'fail';
return
end
handles.data = data;
num = length(data);
for j = 1:num
if ( handles.data(j).ITD > 0 )
prepend = zeros( handles.data(j).ITD, 1 );
append = prepend;
ch1 = [ prepend; handles.data(j).IR(:,1) ];
ch2 = [ handles.data(j).IR(:,2); append ];
handles.data(j).IR = [ch1 ch2];
elseif ( handles.data(j).ITD < 0 )
prepend = zeros( handles.data(j).ITD, 1 );
append = prepend;
ch1 = [ handles.data(j).IR(:,1); append ];
ch2 = [ prepend; handles.data(j).IR(:,2) ];
handles.data(j).IR = [ch1 ch2];
end
end
handles.specs = specs;
handles.numSeries = 1;
handles.sizeLoadedData = size(data,2);
% check size of loaded data
if (handles.sizeLoadedData >= 1)
set(handles.forwardButton, 'Enable', 'on');
end
set(handles.backButton, 'Enable', 'off');
handles.internal = exist('app','var');
if (handles.internal) % if we're loading a file made in ScanIR, the 'app' struct should exist in the load file
handles.app = app;
% BRIR flag
if (handles.app.inMode == 2 && handles.app.irLength/handles.specs.sampleRate >= 1)
handles.app.isBRIR = true;
end
if (app.inMode == 1)
modeString = 'Mono IR ';
elseif (app.inMode == 2)
modeString = 'HRIR ';
if handles.app.isBRIR
modeString = 'BRIR ';
end
if (handles.app.sorted == 1)
set(handles.hrir_panel, 'Visible', 'off');
disp('1');
else
set(handles.hrir_panel, 'Visible', 'on');
set(handles.hrir_panel,'Title',['HRIR Locations for ' num2str(handles.app.currID) '-' num2str(handles.app.currID + handles.app.npositions(handles.numSeries) - 1)]);
if handles.app.isBRIR
set(handles.hrir_panel,'Title',['BRIR Locations for ' num2str(handles.app.currID) '-' num2str(handles.app.currID + handles.app.npositions(handles.numSeries) - 1)]);
end
set(handles.az_start,'String', num2str(handles.app.azPositionData(handles.numSeries,1)));
set(handles.az_interval,'String', num2str(handles.app.azPositionData(handles.numSeries,2)));
set(handles.az_end,'String', num2str(handles.app.azPositionData(handles.numSeries,3)));
set(handles.el_start,'String', num2str(handles.app.elPositionData(handles.numSeries,1)));
set(handles.el_interval,'String', num2str(handles.app.elPositionData(handles.numSeries,2)));
set(handles.el_end,'String', num2str(handles.app.elPositionData(handles.numSeries,3)));
end
elseif (app.inMode == 3)
modeString = 'Multichannel IR';
end
if (handles.app.sigLength == 1)
set(handles.sigLengthDisp, 'String', strcat(num2str(handles.app.sigLength),' second'));
else
set(handles.sigLengthDisp, 'String', strcat(num2str(handles.app.sigLength),' seconds'));
end
set(handles.irLengthDisp, 'String', strcat(num2str(handles.app.irLength), ' samples'));
set(handles.outChannelEdit, 'String',handles.app.outchl);
else % if we're loading HRIRs from a different database with no ScanIR-specific 'app' struct
set(handles.hrir_panel, 'Visible', 'off');
modeString = 'HRIR ';
if handles.app.isBRIR
modeString = 'BRIR ';
end
handles.app.numInputChls = 1:2;
disp('Loading HRIR database - you must have 2 active input channels to record extra HRIR positions.');
if handles.app.isBRIR
modeString = 'BRIR ';
end
handles.app.inMode = 2;
handles.app.outMode = 1;
handles.app.irLength = size(handles.data(1).IR,1);
if ~exist('handles.app.sigLength')
handles.app.sigLength = 1;
disp('No value for test signal length detected - for all measurements, a 1-second signal will be used.');
end
set(handles.sigLengthDisp, 'String', 'unknown');
handles.app.numPlays = 1;
end
handles.app.currID = 1;
set(handles.modeDisp, 'String', modeString);
set(handles.sigTypeDisp, 'String', handles.specs.signalType);
set(handles.irLengthDisp, 'String', strcat(num2str(handles.app.irLength), ' samples'));
guidata(hObject, handles); %updates the handles
updatefields(hObject,handles);
% --- Executes on selection change in plottype_popup.
function plottype_popup_Callback(hObject, eventdata, handles)
if (handles.app.currID<=size(handles.data,2))
plottype = get(handles.plottype_popup, 'Value');
if (plottype == 3 || plottype == 4)
set(handles.chl_popup,'Value',1);
end
handles = displayFields(handles);
plotresponse(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function plottype_popup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in clearButton.
function clearButton_Callback(hObject, eventdata, handles)
cla(handles.plotarea,'reset')
handles = resetFields(handles);
guidata(hObject, handles); %updates the handles
% --- Plotting functions -----------------
function plotresponse(hObject,handles)
cla(handles.plotarea,'reset')
axes(handles.plotarea)
if get(handles.rawplot_checkbox,'Value')
plot_data = handles.data(handles.app.currID).rawIR;
else
plot_data = handles.data(handles.app.currID).IR;
end
plottype = get(handles.plottype_popup, 'Value');
plotchls = get(handles.chl_popup,'Value')-1;
if (plotchls == 0 || plottype == 4|| plottype == 5)
plotchls = 1:length(handles.app.numInputChls);
end
offset = 50;
smth_amnt = [12,8,4,2,1];
fftlens = [512 1024 2048 4096 8192 16384 32768];
fftlen = fftlens(get(handles.fftlen_popup, 'Value'));
lgnd = 0;
%plot
if (plottype == 1) %time domain overlay
handles.fftlen_popup.Enable = 'off';
handles.chl_popup.Enable = 'on';
set(handles.smooth_popup,'Value',1);
handles.smooth_popup.Enable = 'off';
hold off
for i = plotchls
if i<=size(handles.colors,1)
col = handles.colors(i,:);
else
col = rand(1,3);
end
plot(handles.plotarea, plot_data(:, i),...
'Color',col);
hold on
end
xlabel('Time (samples)');
ylabel('Amplitude');
lgnd = 1;
elseif (plottype == 2) %frequency - overlay
% fftlen = 2^nextpow2(length(handles.data(handles.app.currID).IR));
% RESP = 20*log10(abs(fft(handles.data(handles.app.currID).IR(:,:),fftlen)));
handles.fftlen_popup.Enable = 'on';
handles.chl_popup.Enable = 'on';
handles.smooth_popup.Enable = 'on';
hold off
smooth_option = get(handles.smooth_popup,'Value');
if smooth_option > 1
if fftlen >= 8192
set(handles.warn_text,'String','Wait: Computing smothed FFT');
handles.warn_text.Visible = 'on';
drawnow;
end
end
for i = plotchls
if i<=size(handles.colors,1)
col = handles.colors(i,:);
else
col = rand(1,3);
end
r = plot_data(:,i);
[m, ind] = max(abs(r(:)));
strt = max(1, ind-offset);
r = r(strt:min(strt+fftlen-1, length(r)));
RESP = 20*log10(abs(fft(r,fftlen)));
% Apply smoothing if selected
if smooth_option > 1
RESP = LogWinOctaveSmooth(RESP,smth_amnt(smooth_option-1))';
end
semilogx([0:1/(length(RESP)/2):1]*((handles.specs.sampleRate/1000)/2), ...
RESP(1:length(RESP)/2+1), 'Parent', handles.plotarea, ...
'Color',col);
ylim([-60 10]);
hold on
end
xlabel('Frequency (kHz)');
ylabel('dB');
grid on
lgnd = 1;
set(handles.warn_text,'String','');
handles.warn_text.Visible = 'off';
drawnow;
elseif (plottype == 3)
handles.chl_popup.Enable = 'on';
handles.fftlen_popup.Enable = 'off';
set(handles.smooth_popup,'Value',1);
handles.smooth_popup.Enable = 'off';
hold off
interval = 1/handles.specs.sampleRate;
t = [0:interval:(length(handles.data(handles.app.currID).IR(:,1))/handles.specs.sampleRate)-interval];
for i = plotchls
if i<=size(handles.colors,1)
col = handles.colors(i,:);
else
col = rand(1,3);
end
plot(t,handles.data(handles.app.currID).EDC(:,i),...
'Parent',handles.plotarea,'LineWidth',1.5,'Color',col);
hold on
end
xlabel('Time (seconds)');
ylabel('Energy Decay (dB)');
lgnd = 1;
drawnow
elseif (plottype == 4) %time domain cascade
handles.chl_popup.Enable = 'on';
handles.fftlen_popup.Enable = 'off';
set(handles.smooth_popup,'Value',1);
handles.smooth_popup.Enable = 'off';
hold off
maxVal = 0;
for k = plotchls
maxVal = max(maxVal, max(abs(plot_data(:,k))));
end
yScale = .5/maxVal;
for i = plotchls
plot(handles.plotarea, yScale * plot_data(:,i)+i);
hold on;
end
set(handles.plotarea,'ytick', [1:length(handles.app.numInputChls)]);
xlabel('Time (samples)');
ylabel('Channel');
yL = get(handles.plotarea, 'ylim');
lgnd = 1;
elseif (plottype == 5) %frequency - cascade
handles.fftlen_popup.Enable = 'on';
handles.chl_popup.Enable = 'on';
set(handles.smooth_popup,'Value',1);
handles.smooth_popup.Enable = 'off';
if (length(handles.app.numInputChls) == 1)
set(handles.plottype_popup, 'Value', 2);
plotresponse(hObject,handles);
else
resp = [];
for i = plotchls
r = plot_data(:,i);
[m, ind] = max(abs(r(:)));
strt = max(1, ind-offset);
r = r(strt:min(strt+fftlen-1, length(r)));
resp = [resp, r];
end
RESP = 20*log10(abs(fft(resp,fftlen)));
mesh([1:size(RESP(1:length(RESP)/4+1,:),2)], ...
[0:1/(length(RESP)/2):.5]*(handles.specs.sampleRate/1000/2), ...
RESP(1:length(RESP)/4+1,:), ...
'Parent', handles.plotarea);%, [-60, 0]);
caxis([-60 0]);
colorbar;
view([120 20]);
xlabel('Channel');
set(gca,'Xdir','reverse');
ylabel('Frequency (kHz)');
zlabel('dB');
set(handles.plotarea,'xtick', [1:length(handles.app.numInputChls)]);
end
end
%legend
if (lgnd)
str = [];
for i = plotchls
str = strvcat(str, sprintf('Chl %i', i));
end
legend(str);
end
guidata(hObject,handles);
%----------------------------------
function updatefields(hObject,handles)
set(handles.currID_txt, 'String', handles.app.currID);
if (handles.app.currID>size(handles.data,2))
cla(handles.plotarea,'reset');
handles = resetFields(handles);
set(handles.comments_edit, 'String', ' ');
return
end
plotresponse(hObject,handles);
if (handles.app.currID <= size(handles.data,2))
set(handles.az_edit, 'String', num2str(handles.data(handles.app.currID).azimuth));
set(handles.el_edit, 'String', num2str(handles.data(handles.app.currID).elevation));
set(handles.dist_edit, 'String', num2str(handles.data(handles.app.currID).distance));
set(handles.comments_edit, 'String', handles.data(handles.app.currID).comments);
handles = displayFields(handles);
end
guidata(hObject,handles);
function comments_edit_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function comments_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function handles = updateHRIR(hObject, handles) % updates array of az and el values
% update az/el start/int/end
azStart = str2double(get(handles.az_start, 'String'));
azInt = str2double(get(handles.az_interval, 'String') );
elStart = str2double(get(handles.el_start, 'String') );
elInt = str2double(get(handles.el_interval, 'String') );
if (handles.app.outMode == 1) % mono output mode
azEnd = str2double(get(handles.az_end, 'String') );
elEnd = str2double(get(handles.el_end, 'String') );
elseif (handles.app.outMode == 2) % multichannel output mode
azEnd = azStart + (handles.app.npositions(handles.numSeries) - 1) * azInt;
elEnd = elStart + (handles.app.npositions(handles.numSeries) - 1) * elInt;
set(handles.az_end,'String', num2str(azEnd));
set(handles.el_end,'String', num2str(elEnd));
end
if (azInt == 0) % make a full vector of the same azimuth if interval is zero
handles.hrir_az = [azStart];
for k = 1:(handles.app.npositions(handles.numSeries) - 1)
handles.hrir_az = [handles.hrir_az azStart];
end
else % set up vector of azimuths for non-zero intervals
if (azEnd > 360)
azEnd = 360; % max of 360 (later changed to 0 for simplicity)
elseif (azEnd < -360)
azEnd = -360; % min of -360 (later changed to 0)
end
if (azStart > 360) % same bounds for azStart
azStart = 360;
elseif (azStart < -360)
azStart = -360;
end
handles.hrir_az = (azStart:azInt:azEnd);
if (azEnd == 360 || azEnd == -360)
handles.hrir_az(end) = 0;
set(handles.az_end, 'String', '0');
end
if (azStart == 360 || azStart == -360)
handles.hrir_az(1) = 0;
set(handles.az_start, 'String', '0');
end
end
if (elInt == 0) % make a full vector of the same azimuth if interval is zero
handles.hrir_el = [elStart];
for k = 1:(handles.app.npositions(handles.numSeries) - 1)
handles.hrir_el = [handles.hrir_el elStart];
end
else
if (elEnd > 90)
elEnd = 90; % max of 90
set(handles.el_end, 'String', '90');
elseif (elEnd < -90)
elEnd = -90; % min of -90
set(handles.el_end, 'String', '-90');
end
if (elStart > 90) % same bounds for elStart
elStart = 90;
set(handles.el_start, 'String', '90');
elseif (elStart < -90)
elStart = -90;
set(handles.el_start, 'String', '-90');
end
handles.hrir_el = (elStart:elInt:elEnd);
end
% store this series's position data in global arrays
handles.app.azPositionData(handles.numSeries,1) = azStart;
handles.app.azPositionData(handles.numSeries,2) = azInt;
handles.app.azPositionData(handles.numSeries,3) = azEnd;
handles.app.elPositionData(handles.numSeries,1) = elStart;
handles.app.elPositionData(handles.numSeries,2) = elInt;
handles.app.elPositionData(handles.numSeries,3) = elEnd;
% update npositions and seriesInfo
if (handles.app.outMode == 1) % mono output mode
handles.app.npositions(handles.numSeries) = length(handles.hrir_az) * length(handles.hrir_el);
if (handles.numSeries == 1)
handles.app.seriesInfo(1) = handles.app.npositions(handles.numSeries);
else
handles.app.seriesInfo(handles.numSeries) = handles.app.npositions(handles.numSeries) + handles.app.seriesInfo(handles.numSeries-1);
end
set(handles.hrir_panel,'Title',['HRIR Locations for ' num2str(handles.app.currID) '-' num2str(handles.app.currID + handles.app.npositions(handles.numSeries) - 1)]);
if handles.app.isBRIR
set(handles.hrir_panel,'Title',['BRIR Locations for ' num2str(handles.app.currID) '-' num2str(handles.app.currID + handles.app.npositions(handles.numSeries) - 1)]);
end
handles = calcModID(hObject, handles);
elIndex = ceil(handles.modID/length(handles.hrir_az));
if (mod(handles.modID,length(handles.hrir_az)) == 0)
azIndex = length(handles.hrir_az);
else
azIndex = mod(handles.modID,length(handles.hrir_az));
end
elseif (handles.app.outMode == 2) % multichannel output mode
if (handles.numSeries == 1)
handles.app.seriesInfo(1) = handles.app.npositions(handles.numSeries);
else
handles.app.seriesInfo(handles.numSeries) = handles.app.npositions(handles.numSeries) + handles.app.seriesInfo(handles.numSeries-1);
end
handles = calcModID(hObject, handles);
azIndex = handles.modID;
elIndex = handles.modID;
end
currentAz = handles.hrir_az(azIndex);
currentEl = handles.hrir_el(elIndex);
set(handles.az_edit, 'String', num2str(currentAz));
set(handles.el_edit, 'String', num2str(currentEl));
guidata(hObject,handles);
function handles = az_start_fun(hObject,handles)
if (handles.app.outMode == 1)
testVal = str2num(get(handles.az_interval, 'String'));
startVal = str2num(get(handles.az_start, 'String'));
stopVal = str2num(get(handles.az_end, 'String'));
testSign = sign(stopVal - startVal);
if (sign(testVal) ~= testSign && testSign ~= 0)
testVal = testVal * -1;
set(handles.az_interval, 'String', num2str(testVal) );
end
end
if (handles.app.currID<=size(handles.data,2))
overwrite = questdlg('Overwrite position data?', 'Overwrite', 'Yes', 'No', 'Yes');
if strcmp(overwrite, 'No')
set(handles.az_start, 'String', num2str(handles.app.azPositionData(handles.numSeries,1)));
return