0001 function varargout = pzmodel_helper(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 gui_Singleton = 1;
0033 gui_State = struct('gui_Name', mfilename, ...
0034 'gui_Singleton', gui_Singleton, ...
0035 'gui_OpeningFcn', @pzmodel_helper_OpeningFcn, ...
0036 'gui_OutputFcn', @pzmodel_helper_OutputFcn, ...
0037 'gui_LayoutFcn', [] , ...
0038 'gui_Callback', []);
0039 if nargin && ischar(varargin{1})
0040 gui_State.gui_Callback = str2func(varargin{1});
0041 end
0042
0043 if nargout
0044 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0045 else
0046 gui_mainfcn(gui_State, varargin{:});
0047 end
0048
0049
0050
0051
0052 function pzmodel_helper_OpeningFcn(hObject, eventdata, handles, varargin)
0053
0054
0055
0056
0057
0058
0059
0060 handles.output = hObject;
0061
0062
0063 guidata(hObject, handles);
0064
0065
0066
0067
0068
0069 poles = [];
0070 zeros = [];
0071
0072 setappdata(handles.main, 'poles', poles);
0073 setappdata(handles.main, 'zeros', zeros);
0074 setappdata(handles.main, 'filt', []);
0075
0076
0077 function varargout = pzmodel_helper_OutputFcn(hObject, eventdata, handles)
0078
0079
0080
0081
0082
0083
0084 varargout{1} = handles.output;
0085
0086
0087
0088 function pzEdit_Callback(hObject, eventdata, handles)
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 function pzEdit_CreateFcn(hObject, eventdata, handles)
0099
0100
0101
0102
0103
0104
0105 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0106 set(hObject,'BackgroundColor','white');
0107 end
0108
0109
0110
0111 function addPoleBtn_Callback(hObject, eventdata, handles)
0112
0113
0114
0115
0116
0117 strVal = get(handles.pzEdit, 'String');
0118 val = str2num(strVal);
0119 f = val(1);
0120 if length(val) == 2
0121 q = val(2);
0122 p = pole(f,q);
0123 else
0124 p = pole(f);
0125 end
0126
0127 poles = getappdata(handles.main, 'poles');
0128 poles = [poles p];
0129 setappdata(handles.main, 'poles', poles);
0130
0131
0132 updatePolesList(handles);
0133
0134
0135 updateModelResp(handles);
0136
0137
0138
0139 function poleList_Callback(hObject, eventdata, handles)
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 function poleList_CreateFcn(hObject, eventdata, handles)
0150
0151
0152
0153
0154
0155
0156 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0157 set(hObject,'BackgroundColor','white');
0158 end
0159
0160
0161
0162 function addZeroBtn_Callback(hObject, eventdata, handles)
0163
0164
0165
0166
0167
0168 strVal = get(handles.pzEdit, 'String');
0169 val = str2num(strVal);
0170 f = val(1);
0171 if length(val) == 2
0172 q = val(2);
0173 z = zero(f,q);
0174 else
0175 z = zero(f);
0176 end
0177
0178 zeros = getappdata(handles.main, 'zeros');
0179 zeros = [zeros z];
0180 setappdata(handles.main, 'zeros', zeros);
0181
0182
0183 updateZerosList(handles);
0184
0185
0186 updateModelResp(handles);
0187
0188
0189
0190 function zeroList_Callback(hObject, eventdata, handles)
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 function zeroList_CreateFcn(hObject, eventdata, handles)
0201
0202
0203
0204
0205
0206
0207 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0208 set(hObject,'BackgroundColor','white');
0209 end
0210
0211
0212 function updatePolesList(handles)
0213
0214 poles = getappdata(handles.main, 'poles');
0215 pstr = [];
0216 for n=1:length(poles)
0217 p = poles(n);
0218 f = get(p, 'f');
0219 q = get(p, 'q');
0220 if q>0.5
0221 pstr = strvcat(pstr, sprintf('%2.2f Hz Q=%2.2f', f, q));
0222 else
0223 pstr = strvcat(pstr, sprintf('%2.2f Hz', f));
0224 end
0225 end
0226
0227 if isempty(poles)
0228 set(handles.poleList, 'String', ' ');
0229 else
0230 set(handles.poleList, 'String', pstr);
0231 end
0232 set(handles.poleList, 'Value', 1);
0233
0234
0235 function updateZerosList(handles)
0236
0237 zeros = getappdata(handles.main, 'zeros');
0238 pstr = [];
0239 for n=1:length(zeros)
0240 p = zeros(n);
0241 f = get(p, 'f');
0242 q = get(p, 'q');
0243 if q>0.5
0244 pstr = strvcat(pstr, sprintf('%2.2f Hz Q=%2.2f', f, q));
0245 else
0246 pstr = strvcat(pstr, sprintf('%2.2f Hz', f));
0247 end
0248 end
0249
0250 if isempty(zeros)
0251 set(handles.zeroList, 'String', ' ');
0252 else
0253 set(handles.zeroList, 'String', pstr);
0254 end
0255 set(handles.zeroList, 'Value', 1);
0256
0257
0258 function updateModelResp(handles)
0259
0260 gain = str2double(get(handles.gainEdit, 'String'));
0261 poles = getappdata(handles.main, 'poles');
0262 zeros = getappdata(handles.main, 'zeros');
0263
0264 pzm = pzmodel(gain, poles, zeros);
0265
0266 f1s = get(handles.f1Edit, 'String');
0267 f2s = get(handles.f2Edit, 'String');
0268 nfs = get(handles.nfEdit, 'String');
0269 fss = get(handles.fsEdit, 'String');
0270
0271 if isempty(f1s)
0272 f1 = getlowerFreq(pzm)/10;
0273 else
0274 f1 = str2double(f1s);
0275 end
0276 if isempty(f2s)
0277 f2 = getupperFreq(pzm)*10;
0278 else
0279 f2 = str2double(f2s);
0280 end
0281 if isempty(nfs)
0282 nf = 1000;
0283 else
0284 nf = str2double(nfs);
0285 end
0286
0287
0288 a = resp(pzm, f1, f2, nf);
0289 d = a.data;
0290 mx = d.x;
0291 my = d.y;
0292
0293
0294 if isempty(fss)
0295 filt = miir(plist([param('pzmodel', pzm)]));
0296 else
0297 filt = miir(plist([param('pzmodel', pzm) param('fs', str2double(fss))]));
0298 end
0299 setappdata(handles.main, 'filt', filt);
0300 set(handles.filterStrEdit, 'String', string(pzm));
0301
0302 filtresp = resp(filt, plist(param('f', mx.')));
0303 d = filtresp.data;
0304 fx = d.x;
0305 fy = d.y;
0306
0307 axes(handles.magAxes)
0308 loglog(mx,abs(my), fx, abs(fy), 'r--')
0309 grid on;
0310 axis tight
0311 xlabel('');
0312 ylabel('Magnitude');
0313 legend('Pole/Zero model', 'IIR','Location','SouthWest')
0314
0315 axes(handles.phaseAxes)
0316 semilogx(mx, ltpda_phase(my), fx, ltpda_phase(fy), 'r--')
0317 grid on;
0318 axis tight
0319 xlabel('Frequency [Hz]');
0320 ylabel('Phase [deg]');
0321
0322
0323
0324
0325
0326 function gainEdit_Callback(hObject, eventdata, handles)
0327
0328
0329
0330
0331
0332
0333
0334 updateModelResp(handles)
0335
0336
0337 function gainEdit_CreateFcn(hObject, eventdata, handles)
0338
0339
0340
0341
0342
0343
0344 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0345 set(hObject,'BackgroundColor','white');
0346 end
0347
0348
0349
0350 function f1Edit_Callback(hObject, eventdata, handles)
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 function f1Edit_CreateFcn(hObject, eventdata, handles)
0361
0362
0363
0364
0365
0366
0367 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0368 set(hObject,'BackgroundColor','white');
0369 end
0370
0371
0372
0373 function f2Edit_Callback(hObject, eventdata, handles)
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383 function f2Edit_CreateFcn(hObject, eventdata, handles)
0384
0385
0386
0387
0388
0389
0390 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0391 set(hObject,'BackgroundColor','white');
0392 end
0393
0394
0395
0396 function nfEdit_Callback(hObject, eventdata, handles)
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 function nfEdit_CreateFcn(hObject, eventdata, handles)
0407
0408
0409
0410
0411
0412
0413 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0414 set(hObject,'BackgroundColor','white');
0415 end
0416
0417
0418
0419 function replotBtn_Callback(hObject, eventdata, handles)
0420
0421
0422
0423
0424 updateModelResp(handles)
0425
0426
0427
0428 function fsEdit_Callback(hObject, eventdata, handles)
0429
0430
0431
0432
0433
0434
0435 updateModelResp(handles)
0436
0437
0438
0439 function fsEdit_CreateFcn(hObject, eventdata, handles)
0440
0441
0442
0443
0444
0445
0446 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0447 set(hObject,'BackgroundColor','white');
0448 end
0449
0450
0451
0452 function figureBtn_Callback(hObject, eventdata, handles)
0453
0454
0455
0456
0457 gain = str2double(get(handles.gainEdit, 'String'));
0458 poles = getappdata(handles.main, 'poles');
0459 zeros = getappdata(handles.main, 'zeros');
0460
0461 pzm = pzmodel(gain, poles, zeros);
0462
0463 f1s = get(handles.f1Edit, 'String');
0464 f2s = get(handles.f2Edit, 'String');
0465 nfs = get(handles.nfEdit, 'String');
0466 fss = get(handles.fsEdit, 'String');
0467
0468 if isempty(f1s)
0469 f1 = getlowerFreq(pzm)/10;
0470 else
0471 f1 = str2double(f1s);
0472 end
0473 if isempty(f2s)
0474 f2 = getupperFreq(pzm)*10;
0475 else
0476 f2 = str2double(f2s);
0477 end
0478 if isempty(nfs)
0479 nf = 1000;
0480 else
0481 nf = str2double(nfs);
0482 end
0483
0484
0485 a = resp(pzm, f1, f2, nf);
0486 d = a.data;
0487 mx = d.x;
0488 my = d.y;
0489
0490
0491 if isempty(fss)
0492 filt = miir(plist([param('pzmodel', pzm)]));
0493 else
0494 filt = miir(plist([param('pzmodel', pzm) param('fs', str2double(fss))]));
0495 end
0496
0497
0498 filtresp = resp(filt, plist(param('f', mx.')));
0499 d = filtresp.data;
0500 fx = d.x;
0501 fy = d.y;
0502
0503 figure
0504 subplot(3,1,1:2)
0505 loglog(mx,abs(my), fx, abs(fy), 'r--')
0506 grid on;
0507 axis tight
0508 xlabel('');
0509 ylabel('Magnitude');
0510 legend('Pole/Zero model', 'IIR','Location','SouthWest')
0511
0512 subplot(3,1,3)
0513 semilogx(mx, ltpda_phase(my), fx, ltpda_phase(fy), 'r--')
0514 grid on;
0515 axis tight
0516 xlabel('Frequency [Hz]');
0517 ylabel('Phase [deg]');
0518
0519
0520 function filterStrEdit_Callback(hObject, eventdata, handles)
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 function filterStrEdit_CreateFcn(hObject, eventdata, handles)
0531
0532
0533
0534
0535
0536
0537 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0538 set(hObject,'BackgroundColor','white');
0539 end
0540
0541
0542
0543 function clearBtn_Callback(hObject, eventdata, handles)
0544
0545
0546
0547
0548 setappdata(handles.main, 'poles', []);
0549 setappdata(handles.main, 'zeros', []);
0550
0551
0552 updateZerosList(handles);
0553 updatePolesList(handles);
0554
0555
0556 updateModelResp(handles);
0557
0558
0559
0560 function deletePolesBtn_Callback(hObject, eventdata, handles)
0561
0562
0563
0564
0565
0566 values = get(handles.poleList, 'Value');
0567
0568
0569 poles = getappdata(handles.main, 'poles');
0570
0571 po = [];
0572 for j=1:length(poles)
0573 if j~=values
0574 po = [po poles(j)];
0575 end
0576 end
0577
0578 setappdata(handles.main, 'poles', po);
0579 updatePolesList(handles);
0580
0581
0582 updateModelResp(handles);
0583
0584
0585
0586 function deleteZerosBtn_Callback(hObject, eventdata, handles)
0587
0588
0589
0590
0591
0592 values = get(handles.zeroList, 'Value');
0593
0594 zeros = getappdata(handles.main, 'zeros');
0595 zo = [];
0596 for j=1:length(zeros)
0597 if j~=values
0598 zo = [zo zeros(j)];
0599 end
0600 end
0601
0602 setappdata(handles.main, 'zeros', zo);
0603 updateZerosList(handles);
0604
0605
0606 updateModelResp(handles);
0607
0608
0609
0610 function saveAObtn_Callback(hObject, eventdata, handles)
0611
0612
0613
0614
0615
0616 gain = str2double(get(handles.gainEdit, 'String'));
0617 poles = getappdata(handles.main, 'poles');
0618 zeros = getappdata(handles.main, 'zeros');
0619 pzm = pzmodel(gain, poles, zeros);
0620
0621 f1s = get(handles.f1Edit, 'String');
0622 f2s = get(handles.f2Edit, 'String');
0623 nfs = get(handles.nfEdit, 'String');
0624 fss = get(handles.fsEdit, 'String');
0625
0626 if isempty(f1s)
0627 f1 = getlowerFreq(pzm)/10;
0628 else
0629 f1 = str2double(f1s);
0630 end
0631 if isempty(f2s)
0632 f2 = getupperFreq(pzm)*10;
0633 else
0634 f2 = str2double(f2s);
0635 end
0636 if isempty(nfs)
0637 nf = 1000;
0638 else
0639 nf = str2double(nfs);
0640 end
0641
0642
0643 a = resp(pzm, f1, f2, nf);
0644
0645
0646 [filename, pathname] = uiputfile('*.xml', 'LTPDA XML file (*.xml)', 'Save as');
0647 if isequal(filename,0) || isequal(pathname,0)
0648 else
0649 fname = fullfile(pathname, filename);
0650 save(a, fname);
0651 end
0652