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