Merge remote-tracking branch 'origin/5.5'
[scilab.git] / scilab / modules / signal_processing / macros / wfir_gui.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2011-2014 - INRIA - Serge Steer
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
9
10 function [ok,values,exprs]=wfir_gui(exprs)
11     FT=["lp","hp","bp","sb"]
12     WT=["re","tr","hn","hm","kr","ch","ch"]
13     //  errcatch(-1,'continue')
14     if argn(2)<1 then
15         exprs=["""lp""";
16         """re"""
17         "48";
18         "1";
19         "0.05"
20         "0.45";
21         "0.5"]
22     end
23     ftype=exprs(1)
24     wtype=exprs(2)
25     if execstr("forder="+exprs(3)+";freq_ech="+exprs(4)+";low="+exprs(5)+";high="+exprs(6)+";fp="+exprs(7),"errcatch")<>0 then
26         values=[]
27         exprs=[]
28         ok=%f
29         return
30     end
31     global ret
32     ret=0
33
34     margin_x     = 5;      // Horizontal margin between each elements
35     margin_y     = 5;      // Vertical margin between each elements
36     button_w     = 70;
37     button_h     = 30;
38     frame_h     = 300;
39     frame_w     = 440;
40     label_h      = 20;
41     list_h       = 120;
42     list_w       = 100;
43     list_incr    = list_w;
44     axes_w       = 2*margin_x+frame_w;
45     axes_h       = 2*margin_y+frame_h;// Frame height
46     defaultfont  = "arial"; // Default Font
47
48     fig_id=max(winsid())+1
49     fig = scf(fig_id)
50
51     // Remove Scilab graphics menus & toolbar
52     //  drawlater (bug)
53     delmenu(fig.figure_id, gettext("&File"));
54     delmenu(fig.figure_id, gettext("&Tools"));
55     delmenu(fig.figure_id, gettext("&Edit"));
56     delmenu(fig.figure_id, gettext("&?"));
57     toolbar(fig.figure_id, "off");
58     fig.axes_size       = [axes_w axes_h];
59
60
61     fig.background      = addcolor([0.8 0.8 0.8]);
62     fig.figure_name     = _("WFIR settings");
63     ax=fig.children;
64     ax.background= fig.background ;
65
66     gui=uicontrol( ...
67     "parent"              , fig,...
68     "style"               , "frame",...
69     "units"               , "pixels",...
70     "position"            , [0 0 axes_w axes_h],...
71     "background"          , [1 1 1]*0.8, ...
72     "visible"             , "on");
73
74     yo=axes_h-button_h;
75
76
77     yft=frame_h-margin_y-label_h
78     xft=margin_x;
79     //Filter type selection -----------------------------------------------------------------
80     listf_h       = 80;
81     listf_w       = 150;
82     label_w       = 75;
83     uicontrol( ...
84     "parent"              , gui,...
85     "style"               , "text",...
86     "string"              , _("Filter type"),...
87     "units"               , "pixels",...
88     "position"            , [xft yft label_w label_h],....
89     "fontname"            , defaultfont,...
90     "fontunits"           , "points",...
91     "fontsize"            , 12,...
92     "fontweight"          , "bold", ...
93     "horizontalalignment" , "left", ...
94     "background"          , [1 1 1]*0.8, ...
95     "visible"             , "on");
96     Ftv=uicontrol( ...
97     "parent"              , gui,...
98     "style"               , "edit",...
99     "string"              , ftype,...
100     "units"               , "pixels",...
101     "position"            , [xft+label_w+margin_x yft listf_w-label_w-margin_x label_h],....
102     "fontname"            , defaultfont,...
103     "fontunits"           , "points",...
104     "fontsize"            , 12,...
105     "fontweight"          , "bold", ...
106     "horizontalalignment" , "left", ...
107     "background"          , [1 1 1]*0.8, ...
108     "callback"            , "wfirSetFilterType()", ...
109     "visible"             , "on");
110     Filtertype=uicontrol( ...
111     "parent"              , gui,...
112     "style"               , "listbox",...
113     "string"              , [_("Low pass");_("High pass");_("Band pass");_("Stop Band")],...
114     "value"               , find(FT==evstr(ftype)),...
115     "units"               , "pixels",...
116     "position"            , [xft,yft-listf_h,listf_w,listf_h],...
117     "fontname"            , defaultfont,...
118     "fontunits"           , "points",...
119     "fontsize"            , 12,...
120     "horizontalalignment" , "left", ...
121     "BackgroundColor"     , [1 1 1], ...
122     "ForegroundColor"     , [1 1 1]*0, ...
123     "visible"             , "on", ...
124     "callback"            , "wfirSetFilterType()");
125     set(Ftv,"userdata",list([],Filtertype))
126
127     //Window type selection -----------------------------------------------------------------
128     ywt=yft-listf_h-4*margin_y
129     xwt=xft
130     listw_h       = 140;
131     listw_w       = listf_w;
132     uicontrol( ...
133     "parent"              , gui,...
134     "style"               , "text",...
135     "string"              , _("Window type"),...
136     "units"               , "pixels",...
137     "position"            , [xwt ywt label_w label_h],....
138     "fontname"            , defaultfont,...
139     "fontunits"           , "points",...
140     "fontsize"            , 12,...
141     "fontweight"          , "bold", ...
142     "horizontalalignment" , "left", ...
143     "background"          , [1 1 1]*0.8, ...
144     "visible"             , "on");
145     Wtv=uicontrol( ...
146     "parent"              , gui,...
147     "style"               , "edit",...
148     "string"              , wtype,...
149     "units"               , "pixels",...
150     "position"            , [xwt+label_w+margin_x ywt listw_w-label_w-margin_x label_h],....
151     "fontname"            , defaultfont,...
152     "fontunits"           , "points",...
153     "fontsize"            , 12,...
154     "fontweight"          , "bold", ...
155     "horizontalalignment" , "left", ...
156     "background"          , [1 1 1]*0.8, ...
157     "callback"            , "wfirSetWindowType()",...
158     "visible"             , "on");
159
160
161
162     W=[_("Rectangular");_("Triangular");_("Hanning");
163     _("Hamming");_("Kaiser");_("Chebychev main lobe");_("Chebychev side lobe")];
164     index=find(WT==evstr(wtype))
165     if index==[] then index=1,end//for debug
166     Windowtype=uicontrol( ...
167     "parent"              , gui,...
168     "style"               , "listbox",...
169     "string"              , W,...
170     "value"               , find(WT==evstr(wtype)),...
171     "units"               , "pixels",...
172     "position"            , [xwt,ywt-listw_h,listw_w,listw_h],...
173     "fontname"            , defaultfont,...
174     "fontunits"           , "points",...
175     "fontsize"            , 12,...
176     "horizontalalignment" , "left", ...
177     "BackgroundColor"     , [1 1 1], ...
178     "ForegroundColor"     , [1 1 1]*0, ...
179     "visible"             , "on", ...
180     "callback"            , "wfirSetWindowType()");
181     set(Wtv,"userdata",list([],Windowtype))
182
183     //Sampling Frequency -----------------------------------------------------------------
184     ysf=frame_h-margin_y-label_h
185     xsf=xft+listf_w+2*margin_x
186     labelsf_w=180
187     editsf_w=80
188     sfreq=1
189     uicontrol( ...
190     "parent"              , gui,...
191     "style"               , "text",...
192     "string"              ,  _("Sampling Frequency (Hz)"),...
193     "units"               , "pixels",...
194     "position"            , [xsf ysf labelsf_w label_h],....
195     "fontname"            , defaultfont,...
196     "fontunits"           , "points",...
197     "fontsize"            , 12,...
198     "fontweight"          , "bold", ...
199     "horizontalalignment" , "left", ...
200     "background"          , [1 1 1]*0.8, ...
201     "visible"             , "on");
202     Sfreq =uicontrol( ...
203     "parent"              , gui,...
204     "style"               , "edit",...
205     "string"               , exprs(4),...
206     "units"               , "pixels",...
207     "position"            , [xsf+margin_x+labelsf_w,ysf,editsf_w,label_h],...
208     "fontname"            , defaultfont,...
209     "fontunits"           , "points",...
210     "fontsize"            , 12,...
211     "horizontalalignment" , "left", ...
212     "BackgroundColor"     , [1 1 1], ...
213     "ForegroundColor"     , [1 1 1]*0, ...
214     "callback"            , "wfirSetSamplingFrequency()", ...
215     "userdata"            ,sfreq,...
216     "visible"             , "on")
217     //Filter order -----------------------------------------------------------------
218     yfo=ysf-margin_y-label_h
219     xfo=xsf
220     labelfo_w=labelsf_w;
221     editfo_w= editsf_w;
222     scroll_w=160
223     uicontrol( ...
224     "parent"              , gui,...
225     "style"               , "text",...
226     "string"              ,  _("Filter Order"),...
227     "units"               , "pixels",...
228     "position"            , [xfo yfo labelfo_w label_h],....
229     "fontname"            , defaultfont,...
230     "fontunits"           , "points",...
231     "fontsize"            , 12,...
232     "fontweight"          , "bold", ...
233     "horizontalalignment" , "left", ...
234     "background"          , [1 1 1]*0.8, ...
235     "visible"             , "on");
236     Forderv=uicontrol( ...
237     "parent"              , gui,...
238     "style"               , "edit",...
239     "string"              , exprs(3),...
240     "units"               , "pixels",...
241     "position"            , [xfo,yfo-label_h-margin_y,editfo_w,label_h],...
242     "fontname"            , defaultfont,...
243     "fontunits"           , "points",...
244     "fontsize"            , 12,...
245     "horizontalalignment" , "left", ...
246     "BackgroundColor"     , [1 1 1], ...
247     "ForegroundColor"     , [1 1 1]*0, ...
248     "callback"            , "wfirValue2Sliderpos()", ...
249     "visible"             , "on")
250
251     Forders=uicontrol( ...
252     "parent"              , gui,...
253     "style"               , "slider",...
254     "min"                 , 1,...
255     "max"                 , 1000,...
256     "sliderstep"          , [1 100],...
257     "value"               , forder,...
258     "units"               , "pixels",...
259     "position"            , [xfo+editfo_w+margin_x,yfo-label_h-margin_y,scroll_w,label_h],...
260     "fontname"            , defaultfont,...
261     "fontunits"           , "points",...
262     "fontsize"            , 12,...
263     "horizontalalignment" , "left", ...
264     "BackgroundColor"     , [1 1 1], ...
265     "ForegroundColor"     , [1 1 1]*0, ...
266     "userdata"            , list(Forderv,1), ...
267     "callback"            , "wfirSliderpos2ValueI()",...
268     "visible"             , "on")
269     set(Forderv,"userdata",Forders);
270
271     //Low cutoff frequency -----------------------------------------------------------------
272     ylcf=yfo-2*(margin_y+label_h)
273     xlcf=xsf
274     labellcf_w=labelsf_w;
275     editlcf_w= editsf_w;
276     if ftype=="""hp""" then visible="off";else visible="on";end
277     Lcfl=uicontrol( ...
278     "parent"              , gui,...
279     "style"               , "text",...
280     "visible"             , visible,...
281     "string"              ,  _("Low cutoff frequency (Hz)"),...
282     "units"               , "pixels",...
283     "position"            , [xlcf ylcf labellcf_w label_h],....
284     "fontname"            , defaultfont,...
285     "fontunits"           , "points",...
286     "fontsize"            , 12,...
287     "fontweight"          , "bold", ...
288     "horizontalalignment" , "left", ...
289     "background"          , [1 1 1]*0.8);
290     fact=1000;
291     Lcfv=uicontrol( ...
292     "parent"              , gui,...
293     "style"               , "edit",...
294     "visible"             , visible,...
295     "units"               , "pixels",...
296     "string"              , exprs(5),...
297     "position"            , [xlcf,ylcf-label_h-margin_y,editlcf_w,label_h],...
298     "fontname"            , defaultfont,...
299     "fontunits"           , "points",...
300     "fontsize"            , 12,...
301     "horizontalalignment" , "left", ...
302     "BackgroundColor"     , [1 1 1], ...
303     "ForegroundColor"     , [1 1 1]*0, ...
304     "callback"            , "wfirValue2Sliderpos()")
305     Lcfs=uicontrol( ...
306     "parent"              , gui,...
307     "style"               , "slider",...
308     "visible"             , visible,...
309     "min"                 , 0,...
310     "max"                 , 500,...
311     "sliderstep"          , [1 10],...
312     "value"               , low*fact,...
313     "units"               , "pixels",...
314     "position"            , [xlcf+editlcf_w+margin_x,ylcf-label_h-margin_y,scroll_w,label_h],...
315     "fontname"            , defaultfont,...
316     "fontunits"           , "points",...
317     "fontsize"            , 12,...
318     "horizontalalignment" , "left", ...
319     "BackgroundColor"     , [1 1 1], ...
320     "ForegroundColor"     , [1 1 1]*0, ...
321     "userdata"            , list(Lcfv,fact), ...
322     "callback"            , "wfirSliderpos2Value()")
323     set(Lcfv,"userdata",Lcfs);
324
325     //High cutoff frequency -----------------------------------------------------------------
326     yhcf=ylcf-2*(margin_y+label_h)
327     xhcf=xsf
328     labelhcf_w=labelsf_w;
329     edithcf_w= editsf_w;
330     if ftype=="""lp""" then visible="off";else visible="on";end
331     Hcfl=uicontrol( ...
332     "parent"              , gui,...
333     "style"               , "text",...
334     "visible"             , visible,...
335     "string"              ,  _("High cutoff frequency (Hz)"),...
336     "units"               , "pixels",...
337     "position"            , [xhcf yhcf labelhcf_w label_h],....
338     "fontname"            , defaultfont,...
339     "fontunits"           , "points",...
340     "fontsize"            , 12,...
341     "fontweight"          , "bold", ...
342     "horizontalalignment" , "left", ...
343     "background"          , [1 1 1]*0.8);
344     fact=1000;
345     Hcfv=uicontrol( ...
346     "parent"              , gui,...
347     "style"               , "edit",...
348     "visible"             , visible,...
349     "units"               , "pixels",...
350     "string"              , exprs(6),...
351     "position"            , [xhcf,yhcf-label_h-margin_y,edithcf_w,label_h],...
352     "fontname"            , defaultfont,...
353     "fontunits"           , "points",...
354     "fontsize"            , 12,...
355     "horizontalalignment" , "left", ...
356     "BackgroundColor"     , [1 1 1], ...
357     "ForegroundColor"     , [1 1 1]*0, ...
358     "callback"            , "wfirValue2Sliderpos()")
359     Hcfs=uicontrol( ...
360     "parent"              , gui,...
361     "style"               , "slider",...
362     "visible"             , visible,...
363     "min"                 , 0,...
364     "max"                 , 500,...
365     "sliderstep"          , [1 10],...
366     "value"               , high*fact,...
367     "units"               , "pixels",...
368     "position"            , [xhcf+edithcf_w+margin_x,yhcf-label_h-margin_y,scroll_w,label_h],...
369     "fontname"            , defaultfont,...
370     "fontunits"           , "points",...
371     "fontsize"            , 12,...
372     "horizontalalignment" , "left", ...
373     "BackgroundColor"     , [1 1 1], ...
374     "ForegroundColor"     , [1 1 1]*0, ...
375     "userdata"            , list(Hcfv,fact), ...
376     "callback"            , "wfirSliderpos2Value()")
377     set(Hcfv,"userdata",Hcfs);
378
379
380     //Chebychev or Kaiser window parameter -----------------------------------------------------------------
381     yfp=yhcf-2*(margin_y+label_h)
382     xfp=xsf
383     labelfp_w=labelsf_w;
384     editfp_w= editsf_w;
385     if and(wtype<>["""kr""" """ch"""]) then visible="off";else visible="on";end
386     Fpl=uicontrol( ...
387     "parent"              , gui,...
388     "style"               , "text",...
389     "visible"             , visible,...
390     "string"              ,  _("Parameter"),...
391     "units"               , "pixels",...
392     "position"            , [xfp yfp labelfp_w label_h],....
393     "fontname"            , defaultfont,...
394     "fontunits"           , "points",...
395     "fontsize"            , 12,...
396     "fontweight"          , "bold", ...
397     "horizontalalignment" , "left", ...
398     "background"          , [1 1 1]*0.8);
399     fact=1000;
400
401     Fpv=uicontrol( ...
402     "parent"              , gui,...
403     "style"               , "edit",...
404     "visible"             , visible,...
405     "units"               , "pixels",...
406     "string"              , exprs(7),...
407     "position"            , [xfp,yfp-label_h-margin_y,editfp_w,label_h],...
408     "fontname"            , defaultfont,...
409     "fontunits"           , "points",...
410     "fontsize"            , 12,...
411     "horizontalalignment" , "left", ...
412     "BackgroundColor"     , [1 1 1], ...
413     "ForegroundColor"     , [1 1 1]*0, ...
414     "callback"            , "wfirValue2Sliderpos()")
415     Fps=uicontrol( ...
416     "parent"              , gui,...
417     "style"               , "slider",...
418     "visible"             , visible,...
419     "min"                 , 0,...
420     "max"                 , 500,...
421     "sliderstep"          , [1 10],...
422     "value"               , fp*fact,...
423     "units"               , "pixels",...
424     "position"            , [xfp+editfp_w+margin_x,yfp-label_h-margin_y,scroll_w,label_h],...
425     "fontname"            , defaultfont,...
426     "fontunits"           , "points",...
427     "fontsize"            , 12,...
428     "horizontalalignment" , "left", ...
429     "BackgroundColor"     , [1 1 1], ...
430     "ForegroundColor"     , [1 1 1]*0, ...
431     "userdata"            , list(Fpv,fact), ...
432     "callback"            , "wfirSliderpos2Value()")
433
434     set(Fpv,"userdata",Fps);
435
436
437     set(Filtertype,"userdata",list([Lcfl Lcfv Lcfs;Hcfl Hcfv Hcfs],Ftv))
438     set(Windowtype,"userdata",list([Fpl Fpv Fps], Wtv))
439
440     //Display -----------------------------------------------------------------
441     yd=yfp-2*(margin_y+label_h)
442     xd=xsf
443     labeld_w=40
444     uicontrol( ...
445     "parent"              , gui,...
446     "style"               , "text",...
447     "string"              ,  _("View"),...
448     "units"               , "pixels",...
449     "position"            , [xd yd labeld_w label_h],...
450     "fontname"            , defaultfont,...
451     "fontunits"           , "points",...
452     "fontsize"            , 12,...
453     "fontweight"          , "bold", ...
454     "horizontalalignment" , "left", ...
455     "background"          , [1 1 1]*0.8, ...
456     "visible"             , "on");
457     Fview=uicontrol( ...
458     "parent"              , gui,...
459     "style"               , "checkbox",...
460     "units"               , "pixels",...
461     "min"                 , 0, ...
462     "max"                 , 1, ...
463     "value"               , 0, ...
464     "position"            , [xd+margin_x+labeld_w yd label_h label_h],...
465     "background"          , [1 1 1]*0.8, ...
466     "callback"            , "wfirSetFilterView()",...
467     "visible"             , "on");
468
469     ybuttons=margin_y
470     xbuttons=xhcf
471     Ok = uicontrol( ...
472     "parent"              , gui,...
473     "relief"              , "groove",...
474     "style"               , "pushbutton",...
475     "string"              , _("Ok"),...
476     "units"               , "pixels",...
477     "position"            , [xbuttons,ybuttons,button_w button_h],..
478     "fontname"            , "arial",...
479     "fontunits"           , "points",...
480     "fontsize"            , 12,...
481     "fontweight"          , "bold", ...
482     "horizontalalignment" , "center", ...
483     "visible"             , "on", ...
484     "callback"            , "global ret;ret=1;");
485     xbuttons=xbuttons+margin_x+button_w
486     Cancel = uicontrol( ...
487     "parent"              , gui,...
488     "relief"              , "groove",...
489     "style"               , "pushbutton",...
490     "string"              , _("Cancel"),...
491     "units"               , "pixels",...
492     "position"            , [xbuttons,ybuttons,button_w button_h],..
493     "fontname"            , "arial",...
494     "fontunits"           , "points",...
495     "fontsize"            , 12,...
496     "fontweight"          , "bold", ...
497     "horizontalalignment" , "center", ...
498     "visible"             , "on", ...
499     "callback"            , "global ret;ret=2;");
500     xbuttons=xbuttons+margin_x+button_w
501     Help = uicontrol( ...
502     "parent"              , gui,...
503     "relief"              , "groove",...
504     "style"               , "pushbutton",...
505     "string"              , _("Help"),...
506     "units"               , "pixels",...
507     "position"            , [xbuttons,ybuttons,button_w button_h],..
508     "fontname"            , "arial",...
509     "fontunits"           , "points",...
510     "fontsize"            , 12,...
511     "fontweight"          , "bold", ...
512     "horizontalalignment" , "center", ...
513     "visible"             , "on", ...
514     "callback"            , "help wfir");
515     // next used by wfirGetFilterParameters
516     set(gui,"userdata",[Fview,Filtertype,Windowtype,Forderv,Forders,Lcfv,Lcfs,Hcfv,Hcfs,Fpv,Fps,Sfreq,Ftv,Wtv])
517     realtimeinit(0.1);
518     t=0;
519     while ret==0&or(winsid()==fig_id) then
520         //slow down the waiting loop
521         t=t+1;
522         realtime(t);
523     end
524     if ret==1&or(winsid()==fig_id) then
525         ok=%t
526         [ftype,forder,low,high,wtype,fpar,freq_ech]=wfirGetFilterParameters(gui.userdata)
527         values=tlist(["wfir","ftype","forder","low","high","wtype","fpar","freq_ech"],ftype,forder,low,high,wtype,fpar,freq_ech)
528         exprs= wfirGetFilterExprs(gui.userdata)
529         delete(fig)
530     else
531         //user had canceled or closed the gui window
532         ok=%f
533         values=[]
534         exprs=[]
535     end
536     clearglobal ret idle;
537
538 endfunction
539
540 function wfirSliderpos2Value(hs)
541     //hs handle on the slider
542     if argn(2)<1 then hs=gcbo,end
543     ud=hs.userdata
544     hv=ud(1);//handle on edition zone
545     fact=ud(2)// scale factor
546     hv.string=string(hs.value/fact)
547     wfirUpdate(hs)
548 endfunction
549 function wfirSliderpos2ValueI(hs)
550     //hs handle on the slider
551     if argn(2)<1 then hs=gcbo,end
552     hs.value=round(hs.value)
553     ud=hs.userdata
554     hv=ud(1);//handle on edition zone
555     fact=ud(2)// scale factor
556     hv.string=string(hs.value/fact)
557     wfirUpdate(hs)
558 endfunction
559
560 function wfirValue2Sliderpos(hv)
561     // hv handle on edition zone
562     if argn(2)<1 then hv=gcbo,end
563     if execstr("v="+hv.string,"errcatch")==0 then
564         hs=hv.userdata //handle on the slider
565         ud=hs.userdata
566         fact=ud(2)// scale factor
567         hs.value=fact*v
568         hv.ForegroundColor=[0 0 0];
569         wfirUpdate(hv)
570     else
571         hv.ForegroundColor=[1 0 0];
572     end
573 endfunction
574
575 function wfirSetFilterType()
576     FT=["lp","hp","bp","sb"]
577     ud=gcbo.userdata
578
579     if gcbo.style=="edit" then
580         he=gcbo //handle on the edit area
581         hl=ud(2) //handle on the list area
582
583         ok=execstr("k=find("+he.string+"==FT)","errcatch")==0
584         if ~ok|k==[] then
585             he.ForegroundColor=[1 0 0];
586             return
587         else
588             he.ForegroundColor=[0 0 0];
589             hl.value=k
590         end
591     else
592         hl=gcbo //handle on the list area
593         he=ud(2) //handle on the edit area
594         he.string=sci2exp(FT(hl.value),0)
595     end
596     ud=hl.userdata
597     Handles=ud(1)
598     select hl.value
599     case 1 then //low pass
600         Handles(1,:).visible="on";
601         Handles(2,:).visible="off";
602     case 2 then //high pass
603         Handles(1,:).visible="off";
604         Handles(2,:).visible="on";
605     case 3 then //band pass
606         Handles(1,:).visible="on";
607         Handles(2,:).visible="on";
608     case 4 then //stop band
609         Handles(1,:).visible="on";
610         Handles(2,:).visible="on";
611     end
612     wfirUpdate(gcbo)
613 endfunction
614
615 function wfirSetWindowType()
616     WT=["re","tr","hn","hm","kr","ch","ch"]
617     ud=gcbo.userdata
618     if gcbo.style=="edit" then
619         he=gcbo //handle on the edit area
620         hl=ud(2) //handle on the list area
621         ok=execstr("k=find("+he.string+"==WT)","errcatch")==0
622
623         if ~ok|k==[] then
624             he.ForegroundColor=[1 0 0];
625             return
626         else
627             he.ForegroundColor=[0 0 0];
628             hl.value=k
629         end
630     else
631         hl=gcbo //handle on the list area
632         he=ud(2) //handle on the edit area
633         he.string=sci2exp(WT(hl.value),0)
634     end
635
636     ud=hl.userdata
637     Handles=ud(1)
638     k=hl.value
639     if and(k<>[5 6 7]) then
640         Handles.visible="off"
641     else
642         label=Handles(1)
643         value=Handles(2)
644         scroll=Handles(3)
645
646         if k==5 then //Kaiser
647             label.string="Beta"
648             label.visible="on";
649             value.visible="on";
650
651             init=2.5;fact=100;
652             value.string=string(init)
653             scroll.min=0
654             scroll.max=10*fact
655             scroll.value=init*fact
656             d=scroll.userdata;d(2)=fact
657             set(scroll,"userdata",d)
658             scroll.visible="on";
659         elseif k==6 then //Chebychev, main lobe constraint df in [0 0.5]
660             label.string=_("Window main lobe width")
661             label.visible="on";
662             value.visible="on";
663             init=0.2;fact=1000;
664             value.string=string(init)
665             scroll.min=1d-10*fact
666             scroll.max=0.5*fact
667             scroll.value=init*fact
668             d=scroll.userdata;d(2)=fact
669             set(scroll,"userdata",d)
670             scroll.visible="on";
671
672         elseif k==7 then //Chebychev, side lobe constraint dp>0
673             label.string=_("maximum side-lobe height")
674             label.visible="on";
675             value.visible="on";
676             init=0.001;fact=1000;
677             value.string=string(init)
678             scroll.min=1d-10*fact
679             scroll.max=1*fact
680             scroll.value=init*fact
681             d=scroll.userdata;d(2)=fact
682             set(scroll,"userdata",d)
683             scroll.visible="on";
684         end
685     end
686     wfirUpdate(gcbo)
687 endfunction
688
689 function wfirSetFilterView()
690     h=gcbo //handle on the view check box
691     if h.value==0 //disable
692         fig=h.userdata
693         execstr("delete(fig)","errcatch")
694         h.userdata=[]
695     else //enable
696         nwin=size(winsid(),"*")
697         if nwin==0 then win=1;else win=max(winsid())+1;end
698         fig=scf(win)
699         a=gca();
700         a.x_label.text=_("frequency (Hz)")
701         a.y_label.text=_("Magnitude (dB)")
702         a.axes_visible="on"
703         a.grid=ones(1,2)*color("gray");
704         xpoly([],[])
705         set(h,"userdata",fig)
706         clearglobal idle
707         wfirUpdate(gcbo)
708     end
709 endfunction
710
711 function wfirSetSamplingFrequency()
712     Sfreq=gcbo
713     ok=execstr("newfreq="+Sfreq.string,"errcatch")==0
714     ok=ok&newfreq>0
715
716     if ok then
717         Sfreq.ForegroundColor=[0 0 0];
718         curfreq=Sfreq.userdata
719         gui=gcbo.parent
720         H=gui.userdata
721         //H=[Fview,Filtertype,Windowtype,Forderv,Forders,Lcfv,Lcfs,Hcfv,Hcfs,Fpv,Fps,Sfreq,Ftv,Wtv]
722         //update low and high cutoff frequencies
723         r=newfreq/curfreq
724         Lcfv=H(6),Lcfs=H(7),Hcfv=H(8),Hcfs=H(9)
725
726         Lcfs.min=Lcfs.min*r
727         Lcfs.max=Lcfs.max*r
728         Lcfs.value=Lcfs.value*r
729         Lcfv.string=string(evstr(Lcfv.string)*r)
730
731         Hcfs.min=Hcfs.min*r
732         Hcfs.max=Hcfs.max*r
733         Hcfs.value=Hcfs.value*r
734         Hcfv.string=string(evstr(Hcfv.string)*r)
735
736         Sfreq.userdata=newfreq
737         wfirUpdate(gcbo)
738     else
739         Sfreq.ForegroundColor=[1 0 0];
740     end
741
742 endfunction
743
744 function [ftype,forder,low,high,wtype,fpar,freq_ech]=wfirGetFilterParameters(H)
745     //  low,high,freq_ech are returned in Hz
746     //H=[Fview,Filtertype,Windowtype,Forderv,Forders,Lcfv,Lcfs,Hcfv,Hcfs,Fpv,Fps,Sfreq,Ftv,Wtv]
747     FT=["lp","hp","bp","sb"]
748     WT=["re","tr","hn","hm","kr","ch","ch"]
749     Filtertype=H(2);
750     Windowtype=H(3);
751     Forderv=H(4);
752     Lcfv=H(6);
753     Hcfv=H(8);
754     Fpv=H(10);
755     Sfreq=H(12)
756     ftype=FT(Filtertype.value)
757     wtype=WT(Windowtype.value)
758     forder=evstr(Forderv.string)
759     if or(ftype==["hp" "sb"]) then
760         //force odd ordrer
761         if 2*int(forder/2)==forder then forder=forder+1,end
762     end
763     freq_ech=evstr(Sfreq.string)
764     if ftype=="lp" then
765         low=evstr(Lcfv.string)
766         high=0
767     elseif ftype=="hp" then
768         low=evstr(Hcfv.string)
769         high=0
770     else
771         low=evstr(Lcfv.string)
772         high=evstr(Hcfv.string)
773     end
774     if Windowtype.value==5 //Kaiser
775         fpar=[evstr(Fpv.string) 0]
776     elseif Windowtype.value==6 //Chebychev, main lobe constraint df in [0 0.5]
777         fpar=[-1 evstr(Fpv.string)]
778     elseif Windowtype.value==7  //Chebychev, side lobe constraint dp>0
779         fpar=[evstr(Fpv.string) -1]
780     else
781         fpar=[-1 -1]
782     end
783 endfunction
784
785 function  wfirUpdate(h)
786     global idle
787     if ~idle then return,end
788     idle=%f
789     gui=h.parent
790     H=gui.userdata
791     //  H=[Fview,Filtertype,Windowtype,Forderv,Forders,Lcfv,Lcfs,Hcfv,Hcfs,Fpv,Fps,Sfreq,Ftv,Wtv]
792     if H(1).value==1 then
793         old=gcf()
794         [ftype,forder,low,high,wtype,fpar,freq_ech]= ...
795         wfirGetFilterParameters(H)
796         cfreq=[low,high]/freq_ech;
797         [filt,wfm,fr]=wfir(ftype,forder,cfreq,wtype,fpar);
798         fig=H(1).userdata
799         if ~is_handle_valid(fig) then
800             //the window has been closed by user
801             fig=scf(max(winsid())+1)
802             a=gca();
803             a.x_label.text=_("frequency (Hz)")
804             a.y_label.text=_("Magnitude (dB)")
805             a.axes_visible="on"
806             a.grid=ones(1,2)*color("gray");
807             xpoly([],[])
808             set(H(1),"userdata",fig)
809         end
810         eps=1d-6;
811         frq=linspace(eps,0.5-eps,10*size(filt,"*"))
812         [frq,repf]=repfreq(syslin("d",poly(filt,"z","c"),1),frq)
813         scf(fig);drawlater()
814         a=gca();p=a.children
815         frq=frq(:)*freq_ech
816         m=20*log10(abs(repf(:)))
817         a.data_bounds=[min(frq) min(m);max(frq) max(m)]
818         p.data=[frq m];
819         drawnow()
820         scf(old)
821         clearglobal idle
822     end
823 endfunction
824
825 function exprs= wfirGetFilterExprs(H)
826     // H=[Fview,Filtertype,Windowtype,Forderv,Forders,Lcfv,Lcfs,Hcfv,Hcfs,Fpv,Fps,Sfreq,Ftv,Wtv]
827     FT=["lp","hp","bp","sb"]
828     WT=["re","tr","hn","hm","kr","ch","ch"]
829     Filtertype=H(2);
830     Windowtype=H(3);
831     Forderv=H(4);
832     Lcfv=H(6);
833     Hcfv=H(8);
834     Fpv=H(10);
835     Sfreq=H(12)
836     exprs=[Ftv.string;
837     Wtv.string;
838     Forderv.string;
839     Sfreq.string;
840     Lcfv.string
841     Hcfv.string;
842     Fpv.string]
843 endfunction