[0f6817]: kwave / filter.cpp Maximize Restore History

Download this file

filter.cpp    601 lines (521 with data), 16.5 kB

  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
#include <qfiledlg.h>
#include <qkeycode.h>
#include "filter.h"
#include <math.h>
extern QDir *filterDir;
extern QStrList *filterNameList;
const char *allow_double="-0123456789.";
//**********************************************************
Filter::Filter ()
{
num=0;
offset=0;
mult=0;
}
//**********************************************************
Filter::~Filter ()
{
if (offset) delete offset;
if (mult) delete mult;
}
//**********************************************************
int Filter::resize (int newnum)
{
if (newnum!=num) //no need to resize arrays ....
{
int *boffset=new int[newnum];
double *bmult=new double[newnum];
if (boffset&&bmult)
{
for (int i=0;i<newnum;i++) //initialize new arrays
{
bmult[i]=0;
boffset[i]=i;
}
bmult[0]=1;
int less=num;
if (newnum<num) less=newnum;
if (offset) //copy old values....
{
for (int i=0;i<less;i++) boffset[i]=offset[i];
delete offset;
}
if (mult)
{
for (int i=0;i<less;i++) bmult[i]=mult[i];
delete mult;
}
offset=boffset;
mult=bmult;
num=newnum;
return num;
}
else return false;
}
return newnum;
}
//**********************************************************
void Filter::save (QString *name)
{
char buf[80];
printf ("%s\n",name->data());
if (name->find (".filter")==-1) name->append (".filter");
QFile out(name->data());
out.open (IO_WriteOnly);
if (fir) sprintf (buf,"FIR %d\n",num);
else sprintf (buf,"IIR %d\n",num);
out.writeBlock (&buf[0],strlen(buf));
for (int i=0;i<num;i++)
{
sprintf (buf,"%d %e\n",offset[i],((double)mult[i]));
out.writeBlock (&buf[0],strlen(buf));
}
}
//**********************************************************
void Filter::load (QString *name)
{
char buf[120];
QFile *in=new QFile(name->data());
if ((in)&&(in->open (IO_ReadOnly)))
{
int res;
res=in->readLine(buf,120);
while ((res>0)&&((strncmp (buf,"FIR",3)!=0)&&(strncmp(buf,"IIR",3)!=0)))
res=in->readLine(buf,120);
if (res>0)
{
if (strncmp (buf,"FIR",3)==0)
fir=true;
else
fir=false;
int newnum=strtol (&buf[4],0,0);
int x;
int cnt=0;
float y;
if (resize (newnum))
{
while ((in->readLine(buf,120)>0)&&(cnt<newnum))
{
if ((buf[0]!='/')||(buf[0]!='#'))
{
sscanf (buf,"%d %f\n",&x,&y);
offset[cnt]=x;
mult[cnt++]=(int) (y);
}
}
}
}
}
}
//**********************************************************
FilterDialog::FilterDialog (QWidget *par,int rate): QDialog(par,0,true)
{
setCaption (klocale->translate("Choose filter parameters:"));
filter.rate=rate;
ok = new QPushButton (klocale->translate("&Filter"),this);
cancel = new QPushButton (klocale->translate("Cancel"),this);
filterwidget = new FFTWidget (this);
phasewidget = new FFTWidget (this);
load = new QPushButton (klocale->translate("&load filter"),this);
save = new QPushButton (klocale->translate("&save filter"),this);
QToolTip::add( filterwidget,klocale->translate("resulting changes in spectrum"));
QToolTip::add( phasewidget ,klocale->translate("resulting changes in phase"));
phasewidget->setAutoDelete (false);
int bsize=ok->sizeHint().height();
QVBoxLayout *vbox;
bg = new QButtonGroup( this);
bg->setTitle(klocale->translate("Filter Type"));
vbox = new QVBoxLayout(bg, 10);
vbox->addSpacing( bg->fontMetrics().height() );
fir = new QRadioButton( bg );
fir->setText( "&FIR" );
QToolTip::add( fir, klocale->translate("Use normal filtering, e.g. the impulse response is finite !"));
vbox->addWidget(fir);
fir->setMinimumSize(bsize*3,bsize);
iir = new QRadioButton( bg );
iir->setText(klocale->translate ("&IIR"));
vbox->addWidget(iir);
iir->setMinimumSize(bsize*3,bsize);
QToolTip::add( iir, klocale->translate("Use recursive filtering, e.g. the impulse response could be infinite !"));
iir->setMinimumSize( iir->sizeHint());
fir->setChecked (true);
taps=new KIntegerLine (this);
taps->setText ("10");
taplabel=new QLabel (klocale->translate("# of :"),this);
QToolTip::add( taps ,klocale->translate("Number of filter coefficients\n Keep this low and you won't need any coffee break..."));
label=0;
mult=0;
offset=0;
getNTaps (10);
ampx=new ScaleWidget (this,0,rate/2,"Hz");
ampy=new ScaleWidget (this,100,0,"%");
ampcorner=new CornerPatchWidget (this);
phasex=new ScaleWidget (this,0,rate/2,"Hz");
phasey=new ScaleWidget (this,180,-180,"�");
phasecorner=new CornerPatchWidget (this);
cancel->setAccel(Key_Escape);
ok->setFocus ();
connect (ok ,SIGNAL(clicked()),SLOT (accept()));
connect (cancel ,SIGNAL(clicked()),SLOT (reject()));
connect (load ,SIGNAL(clicked()),SLOT (loadFilter()));
connect (fir ,SIGNAL(clicked()),SLOT (refresh()));
connect (iir ,SIGNAL(clicked()),SLOT (refresh()));
connect (save ,SIGNAL(clicked()),SLOT (saveFilter()));
connect (taps ,SIGNAL(textChanged(const char *)),SLOT (setTaps(const char *)));
refresh ();
}
//**********************************************************
void FilterDialog::setMult (int newvalue)
{
char buf [16];
for (int i=0;i<filter.num;i++)
{
if (mult[i]->value()==newvalue)
{
filter.mult[i]=((double)(-1000+newvalue)*-1)/1000;
sprintf (buf,"%3.1f",filter.mult[i]*100);
label[i]->setText (buf);
}
}
refresh ();
}
//**********************************************************
void FilterDialog::setOffset (const char *newvalue)
{
for (int i=0;i<filter.num;i++)
{
if (strcmp (offset[i]->text(),newvalue)==0)
filter.offset[i]=(int)(QString(newvalue)).toLong();
}
refresh ();
}
//**********************************************************
void FilterDialog::refresh ()
{
int max=0;
for (int i=0;i<filter.num;i++) if (max<filter.offset[i]) max=filter.offset[i]; //get maximum filter offset
int points=1;
while (points<max+1) points<<=1; //get number of points for fft...
if (points<1024) points=1024;
// printf ("%d points\n",points);
gsl_fft_complex_wavetable table;
gsl_fft_complex_wavetable_alloc (points,&table);
gsl_fft_complex_init (points,&table);
complex *data=new complex[points];
if (data)
{
for (int i=0;i<points;i++)
{
data [i].real=0;
data [i].imag=0;
}
filter.fir=fir->isChecked();
if (filter.fir)
{
for (int i=0;i<filter.num;i++)
if (filter.mult[i])
data[filter.offset[i]].real=filter.mult[i];
//generate impulse response
}
else
{
data[0].real=filter.mult[0];
for (int j=1;j<points;j++)
for (int i=1;i<filter.num;i++)
if (j-filter.offset[i]>=0) data[j].real+=filter.mult[i]*data[j-filter.offset[i]].real;
}
gsl_fft_complex_forward (data,points,&table); //do fft of the impulse response...
double rea;
double ima;
double maxp=0;
for (int i=0;i<points;i++)
{
rea=data[i].real;
ima=data[i].imag;
rea=sqrt(rea*rea+ima*ima);
if (rea>maxp) maxp=rea;
}
phasewidget->setPhase (data,points,filter.rate);
filterwidget->setSignal (data,points,filter.rate);
filterwidget->refresh();
phasewidget->refresh();
}
gsl_fft_complex_wavetable_free (&table);
}
//**********************************************************
Filter *FilterDialog::getFilter ()
{
return &filter;
}
//**********************************************************
void FilterDialog::loadFilter ()
{
QString name=QFileDialog::getOpenFileName (filterDir->path(),"*.filter",this);
if (!name.isNull())
{
filter.load (&name);
getNTaps (filter.num);
repaint (true);
refresh ();
}
}
//**********************************************************
void FilterDialog::saveFilter ()
{
QString name=QFileDialog::getSaveFileName (filterDir->path(),"*.filter",this);
if (!name.isNull())
filter.save (&name);
}
//**********************************************************
void FilterDialog::getNTaps (int newnum)
{
QSlider **newmult=new QSlider* [newnum];
KIntegerLine **newoffset=new KIntegerLine*[newnum];
QLabel **newlabel=new QLabel*[newnum];
if ((newmult)&&(newoffset)&&(newlabel)&&filter.resize(newnum))
{
if (mult)
{
for (int i=0;i<oldnum;i++) delete mult[i];
delete mult;
}
if (offset)
{
for (int i=0;i<oldnum;i++) delete offset[i];
delete offset;
}
if (label)
{
for (int i=0;i<oldnum;i++) delete label[i];
delete label;
}
mult=newmult;
label=newlabel;
offset=newoffset;
for (int i=0;i<newnum;i++)
{
label[i]=new QLabel ("0.00",this);
offset[i]=new KIntegerLine (this);
mult[i]=new QSlider (0,2000,1,0,QSlider::Vertical,this);
label[i]->show();
mult[i]->show();
offset[i]->show();
oldnum=newnum;
connect (offset[i],SIGNAL(textChanged(const char *)),this,SLOT(setOffset(const char *)));
connect (mult[i],SIGNAL (valueChanged(int)), this,SLOT(setMult(int)));
}
offset[0]->setEnabled (false);
int bsize=ok->sizeHint().height();
int top=h/2;
int he=h-top-bsize*2;
for (int i=0;i<filter.num;i++)
{
label[i]->setGeometry (i*w/filter.num,top,w/filter.num,bsize);
mult[i]->setGeometry (i*w/filter.num,top+bsize,w/filter.num,he-2*bsize);
offset[i]->setGeometry (i*w/filter.num,top+he-bsize,w/filter.num,bsize);
}
refreshView();
int width=bsize*(filter.num+4);
if (width<400) width=400;
setMinimumSize (width,bsize*20);
resize (width,bsize*20);
}
else printf ("error allocating objects, get some memory...\n");
}
//**********************************************************
void FilterDialog::refreshView ()
{
char buf[16];
int ms;
for (int i=0;i<filter.num;i++)
{
mult[i]->setValue (1000-(int)(1000*filter.mult[i]));
ms=filter.offset[i];
sprintf (buf,"%d",ms);
offset[i]->setText (buf);
}
if (filter.fir)
{
fir->setChecked (true);
iir->setChecked (false);
}
else
{
iir->setChecked (true);
fir->setChecked (false);
}
sprintf (buf,"%d",filter.num);
taps->setText (buf);
}
//**********************************************************
void FilterDialog::setTaps (const char *n)
{
int x=1;
if (n) x=strtol(n,0,0);
if (x<2) x=2; //since you need 2... there are other functions for volume...
if (x>50) x=50; //feel free to change, if you have a big screen ...
if (x!=filter.num) getNTaps (x);
repaint ();
refresh ();
}
//**********************************************************
void FilterDialog::resizeEvent (QResizeEvent *)
{
int bsize=ok->sizeHint().height();
int lsize=bsize;
w=this->width();
h=this->height();
int top=h/2;
int he=h-top-bsize*2;
int ty=bsize*2;
int tapl=w-2*ty-2*lsize;
int offs=lsize/2+bsize;
taplabel->setGeometry (tapl+lsize/2,lsize/4,ty,lsize);
taps->setGeometry (tapl+lsize+ty,lsize/4,ty,bsize);
load->setGeometry (tapl+lsize/2,offs,w-tapl-lsize,bsize);
offs+=bsize+lsize/4;
save->setGeometry (tapl+lsize/2,offs,w-tapl-lsize,bsize);
offs+=bsize+lsize/4;
bg->setGeometry (tapl+lsize/2,offs,w-tapl-lsize,top-offs);
filterwidget->setGeometry (bsize,0,tapl-bsize,top/2-bsize);
phasewidget->setGeometry (bsize,top/2+lsize/4,tapl-bsize,top/2-bsize);
phasey->setGeometry (0,0,top/2-bsize,bsize);
ampcorner->setGeometry (0,top/2-bsize,bsize,bsize);
phasecorner->setGeometry (0,top-bsize,bsize,bsize);
ampy->setGeometry (0,0,bsize,top/2-bsize);
phasey->setGeometry (0,top/2,bsize,top/2-bsize);
ampx->setGeometry (bsize,top/2-bsize,tapl-bsize,bsize);
phasex->setGeometry (bsize,top-bsize,tapl-bsize,bsize);
top+=lsize/2;
for (int i=0;i<filter.num;i++)
{
label[i]->setGeometry (i*w/filter.num,top,w/filter.num,bsize);
mult[i]->setGeometry (i*w/filter.num,top+bsize,w/filter.num,he-2*bsize);
offset[i]->setGeometry (i*w/filter.num,top+he-bsize,w/filter.num,bsize);
}
ok->setGeometry (w/10,h-bsize-lsize/2,w*3/10,bsize);
cancel->setGeometry (w*6/10,h-bsize-lsize/2,w*3/10,bsize);
}
//**********************************************************
FilterDialog::~FilterDialog ()
{
}
//**********************************************************
MovingFilterDialog::MovingFilterDialog (QWidget *par=NULL,int num): QDialog(par, 0,true)
{
this->num=num;
setCaption (klocale->translate("Choose Filter Movement :"));
ok = new QPushButton (klocale->translate("Filter"),this);
cancel = new QPushButton (klocale->translate("Cancel"),this);
lowlabel= new QLabel (klocale->translate("Range goes from"),this);
highlabel= new QLabel (klocale->translate("to"),this);
tap=new KIntegerLine (this);
tap->setText ("1");
usecurve=new QCheckBox (klocale->translate("do filtering with changing coefficient"),this);
low=new KRestrictedLine (this);
low->setValidChars (allow_double);
low->setText ("-100 %");
high=new KRestrictedLine (this);
high->setValidChars (allow_double);
high->setText ("100 %");
curve= new CurveWidget (this);
curve->setBackgroundColor (QColor(black) );
high->setEnabled (false);
low->setEnabled (false);
tap->setEnabled (false);
curve->setEnabled (false);
int bsize=ok->sizeHint().height();
setMinimumSize (320,bsize*9);
resize (320,bsize*9);
ok->setAccel (Key_Return);
cancel->setAccel(Key_Escape);
ok->setFocus ();
connect (ok ,SIGNAL(clicked()),SLOT (accept()));
connect (cancel ,SIGNAL(clicked()),SLOT (reject()));
connect (usecurve,SIGNAL(clicked()),SLOT (toggleState()));
}
//**********************************************************
void MovingFilterDialog::toggleState ()
{
if (usecurve->isChecked())
{
high->setEnabled (true);
low->setEnabled (true);
tap->setEnabled (true);
curve->setEnabled (true);
}
else
{
high->setEnabled (false);
low->setEnabled (false);
tap->setEnabled (false);
curve->setEnabled (false);
}
}
//**********************************************************
void MovingFilterDialog::checkTap (const char *text)
{
char buf[16];
int i=strtol (text,0,0);
if (i<0) i=0;
if (i>num) i=num;
sprintf (buf,"%d",num);
tap->setText (buf);
}
//**********************************************************
int MovingFilterDialog::getTap ()
{
return strtol (tap->text(),0,0);
}
//**********************************************************
int MovingFilterDialog::getLow ()
{
return (int) (10*strtod (low->text(),0));
}
//**********************************************************
int MovingFilterDialog::getHigh ()
{
return (int) (10*strtod (high->text(),0));
}
//**********************************************************
int MovingFilterDialog::getState ()
{
return usecurve->isChecked();
}
//**********************************************************
QList<CPoint> *MovingFilterDialog::getPoints ()
{
return curve->getPoints();
}
//**********************************************************
int MovingFilterDialog::getType ()
{
return curve->getInterpolationType();
}
//**********************************************************
void MovingFilterDialog::resizeEvent (QResizeEvent *)
{
int bsize=ok->sizeHint().height();
int offset=0;
curve->setGeometry (width()/20,0,width()*18/20,height()-bsize*5);
usecurve->setGeometry (width()/20,height()-bsize*9/2,usecurve->sizeHint().width(),bsize);
offset+=usecurve->sizeHint().width()+bsize/4;
tap->setGeometry (width()/20+offset,height()-bsize*9/2,width()*18/20-offset,bsize);
offset=0;
lowlabel->setGeometry (width()/20,height()-bsize*3,lowlabel->sizeHint().width(),bsize);
offset+=lowlabel->sizeHint().width()+bsize/4;
low->setGeometry (width()/20+offset,height()-bsize*3,bsize*2,bsize);
offset+=bsize*9/4;
highlabel->setGeometry (width()/20+offset,height()-bsize*3,highlabel->sizeHint().width(),bsize);
offset+=highlabel->sizeHint().width()+bsize/4;
high->setGeometry (width()/20+offset,height()-bsize*3,bsize*2,bsize);
offset+=highlabel->sizeHint().width();
ok->setGeometry (width()/10,height()-bsize*3/2,width()*3/10,bsize);
cancel->setGeometry (width()*6/10,height()-bsize*3/2,width()*3/10,bsize);
}
//**********************************************************
MovingFilterDialog::~MovingFilterDialog ()
{
delete curve ;
}
//**********************************************************