Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[429088]: kwave / sonagram.cpp Maximize Restore History

Download this file

sonagram.cpp    431 lines (384 with data), 12.4 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
#include <qdir.h>
#include "sonagram.h"
#include <qpainter.h>
#include <math.h>
#include <limits.h>
#include <qcursor.h>
#include <kmsgbox.h>
#include "dialogs.h"
#include "sample.h"
#include "main.h"
#include "windowfunction.h"
extern KApplication *app;
extern char *mstotimec (int ms);
//****************************************************************************
ImageView::ImageView (QWidget *parent) : QWidget (parent)
{
this->parent=parent;
image=0;
lh=-1;
lw=-1;
offset=0;
setCursor (crossCursor);
}
//****************************************************************************
ImageView::~ImageView ()
{
}
//****************************************************************************
void ImageView::mouseMoveEvent (QMouseEvent *e)
{
int x=e->pos().x();
if ((x<width)&&(x>=0))
{
int y=e->pos().y();
if ((y>=0)&&(y<height))
emit info ((double)x/width,(double)(height-y)/height);
}
}
//****************************************************************************
int ImageView::getOffset () {return offset;}
int ImageView::getWidth () {return width;}
void ImageView::setImage (QImage *image)
{
this->image=image;
repaint ();
}
//****************************************************************************
void ImageView::setOffset (int offset)
{
if (this->offset!=offset)
{
this->offset=offset;
repaint ();
}
}
//****************************************************************************
void ImageView::paintEvent (QPaintEvent *)
{
height=rect().height();
width=rect().width();
if (image)
{
if ((lh!=height)||((lw!=width)&&(image->width()<width)))
{
if (offset>image->width()-width) offset=image->width()-width;
QWMatrix matrix;
QPixmap newmap;
newmap.convertFromImage (*image,0);
if (image->width()<width)
{
offset=0;
matrix.scale (((float)width)/image->width(),((float)height)/image->height());
}
else
matrix.scale (1,((float)height)/image->height());
map=(newmap.xForm (matrix));
lh=height;
lw=width;
}
emit viewInfo (offset,width,image->width());
bitBlt (this,0,0,&map,offset,0,width,height);
}
}
//****************************************************************************
SonagramContainer::SonagramContainer (QWidget *parent): QWidget (parent)
{
this->view=0;
}
//****************************************************************************
void SonagramContainer::setObjects (ImageView *view,ScaleWidget *x,ScaleWidget *y,CornerPatchWidget *corner,OverViewWidget *overview)
{
this->view=view;
this->xscale=x;
this->yscale=y;
this->corner=corner;
this->overview=overview;
}
//****************************************************************************
SonagramContainer::~SonagramContainer ()
{
}
//****************************************************************************
void SonagramContainer::resizeEvent (QResizeEvent *)
{
if (view)
{
int bsize=(QPushButton("test",this).sizeHint()).height();
view->setGeometry (bsize,0,width()-bsize,height()-bsize*2);
xscale->setGeometry (bsize,height()-bsize*2,width()-bsize,bsize);
overview->setGeometry (bsize,height()-bsize,width()-bsize,bsize);
yscale->setGeometry (0,0,bsize,height()-bsize*2);
corner->setGeometry (0,height()-bsize*2,bsize,bsize);
}
}
//****************************************************************************
SonagramWindow::SonagramWindow (QString *name) : KTopLevelWidget ()
{
KMenuBar *bar= new KMenuBar (this);
QPopupMenu *spectral= new QPopupMenu ();
QPopupMenu *file= new QPopupMenu ();
bar->insertItem (klocale->translate("&File"),file);
bar->insertItem (klocale->translate("&Spectral Data"),spectral);
file->insertItem (klocale->translate("&Import from Bitmap ..."),this,SLOT(load()));
file->insertItem (klocale->translate("&Export to Bitmap ..."),this,SLOT(save()));
spectral->insertItem (klocale->translate("&reTransform to signal"),this,SLOT(toSignal()));
status=new KStatusBar (this,"Frequencies Status Bar");
status->insertItem ("Time: 0 ms ",1);
status->insertItem ("Frequency: 0 Hz ",2);
status->insertItem ("Amplitude: 0 % ",3);
status->insertItem ("Phase: 0 ",4);
mainwidget=new SonagramContainer (this);
view=new ImageView (mainwidget);
connect (view,SIGNAL(info (double,double)),this,SLOT(setInfo(double,double)));
xscale=new ScaleWidget (mainwidget,0,100,"ms");
yscale=new ScaleWidget (mainwidget,100,0,"Hz");
corner=new CornerPatchWidget (mainwidget);
overview=new OverViewWidget (mainwidget);
QObject::connect (overview,SIGNAL(valueChanged(int)),
view,SLOT(setOffset(int)));
QObject::connect (view,SIGNAL(viewInfo(int,int,int)),
this,SLOT(setRange(int,int,int)));
QObject::connect (view,SIGNAL(viewInfo(int,int,int)),
overview,SLOT(setRange(int,int,int)));
mainwidget->setObjects (view,xscale,yscale,corner,overview);
setView (mainwidget);
setStatusBar (status);
setMenu (bar);
QString *windowname=new QString (QString ("Sonagram of ")+QString(name->data()));
setCaption (windowname->data());
resize (480,300);
}
//****************************************************************************
void SonagramWindow::save ()
{
if (image)
{
QString filename = QFileDialog::getSaveFileName("","*.bmp",this);
if ( !filename.isEmpty() ) image->save( filename, "BMP" );
}
}
//****************************************************************************
void SonagramWindow::load ()
{
if (image)
{
QString filename = QFileDialog::getOpenFileName("","*.bmp",this);
printf ("loading %s\n",filename.data());
if (!filename.isNull())
{
printf ("loading %s\n",filename.data());
QImage *newimage=new QImage (filename);
if (newimage)
{
if ((image->height()==newimage->height())
&&(image->width()==newimage->width()))
{
for (int i=0;i<x;i++)
{
for (int j=0;j<points/2;j++)
{
if (data[i])
{
// data[i][j].real;
}
}
}
delete image;
image=newimage;
view->setImage (image);
}
else
{
char buf[64];
delete newimage;
sprintf (buf,"Bitmap must be %dx%d",image->width(),image->height());
KMsgBox::message (this,"Info",buf,2);
}
}
else
KMsgBox::message (this,"Error","Could not open Bitmap",2);
}
}
}
//****************************************************************************
void SonagramWindow::setSignal (double *input,int size, int points,int windowtype,int rate)
{
double rea,ima;
// printf ("size %d points %d windowtype %d\n",size,points,windowtype);
this->length=size;
this->x=(size/points);
this->points=points;
this->rate=rate;
yscale ->setMaxMin (0,rate/2);
xscale ->setMaxMin ((int)(((double)size)/rate*1000),0);
data= new complex *[x];
WindowFunction func (windowtype);
image=new QImage (x,points/2,8,256);
double* windowfunction=func.getFunction(points);
if ((data)&&(image)&&windowfunction)
{
char buf [48];
sprintf (buf,"doing %d %d-point mixed radix fft\'s\n",x,points);
ProgressDialog *dialog=new ProgressDialog (x,buf);
if (dialog)
{
dialog->show();
gsl_fft_complex_wavetable table;
gsl_fft_complex_wavetable_alloc (points,&table);
gsl_fft_complex_init (points,&table);
for (int i=0;i<x;i++)
{
complex *output=new complex[points];
if (output)
{
for (int j=0;j<points;j++)
{
output[j].real=windowfunction[j]*input[i*points+j]; //copy data into complex array
output[j].imag=0;
}
gsl_fft_complex_forward (output,points,&table);
for (int k=0;k<points;k++)
{
rea=output[k].real;
ima=output[k].imag;
rea=sqrt(rea*rea+ima*ima); //get amplitude
if (max<rea) max=rea; //and set maximum for display..
}
dialog->setProgress (i);
}
data[i]=output; //put single spectrum into array of spectra
}
gsl_fft_complex_wavetable_free (&table);
}
delete dialog;
createPalette ();
createImage ();
view->setImage (image);
}
else
{
KMsgBox::message (this,"Info","Out of memory !",2);
if (data) delete data;
if (image) delete image;
}
}
//****************************************************************************
void SonagramWindow::toSignal ()
{
gsl_fft_complex_wavetable table;
gsl_fft_complex_wavetable_alloc (points,&table);
gsl_fft_complex_init (points,&table);
TopWidget *win=new TopWidget (app);
if (win)
{
MSignal *newsig=new MSignal (win,length,rate);
int slopesize=rate/10; //assure 10 Hz for correction signal, this should not be audible
double *slope=new double [slopesize];
if (slope&&newsig)
{
for(int i=0;i<slopesize;i++)
slope[i]=0.5+0.5*cos( ((double) i) * M_PI / slopesize);
win->show();
int *output=newsig->getSample(); //sample data
complex *tmp= new complex [points]; //this window holds the data for ifft and after that part of the signal
if (output&&tmp&&data)
{
for (int i=0;i<x;i++)
{
if (data[i]) memcpy (tmp,data[i],sizeof(complex)*points);
gsl_fft_complex_inverse (tmp,points,&table);
for (int j=0;j<points;j++)
output[i*points+j]=(int)(tmp[j].real*((1<<23)-1));
}
int dif;
int max;
for (int i=1;i<x;i++) //remove gaps between windows
{
max=slopesize;
if (max>length-i*points) max=length-i*points;
dif=output[i*points]-output[i*points-1];
if (dif<2)
for (int j=0;j<max;j++) output[i*points+j]+=(int) (slope[j]*dif); }
win->setSignal (newsig);
if (tmp) delete tmp;
}
else
{
if (newsig) delete newsig;
if (win) delete win;
KMsgBox::message (this,"Info","Out of memory !",2);
}
}
if (slope) delete slope;
}
}
//****************************************************************************
void SonagramWindow::createPalette ()
{
for (int i=0;i<256;i++) image->setColor(i, qRgb(i,i,i) ); //create grayscale palette
}
//****************************************************************************
void SonagramWindow::createImage ()
{
double rea,ima;
if (image&&data)
{
for (int i=0;i<x;i++)
if (data[i])
for (int j=0;j<points/2;j++)
{
rea=data[i][j].real;
ima=data[i][j].imag;
rea=sqrt(rea*rea+ima*ima)/max; //get amplitude and scale to 1
rea=1-((1-rea)*(1-rea));
*(image->scanLine((points/2-1)-j) + i)=255-(int)(rea*255);
}
}
}
//****************************************************************************
SonagramWindow::~SonagramWindow (QWidget *parent,const char *name)
{
if (data)
{
for (int i=0;i<x;i++) if (data[i]) delete data[i];
delete data;
}
if (image) delete image;
}
//****************************************************************************
void SonagramWindow::setInfo (double x,double y)
{
char buf[64];
int col;
if (view->getWidth()>this->x)
col=(int)(x*(this->x-1));
else
col=(int)(view->getOffset()+x*view->getWidth());
sprintf (buf,"Time: %s",mstotimec ((int)(((double) col)*points*10000/rate)));
status->changeItem (buf,1);
sprintf (buf,"Frequency: %d Hz",(int)(y*rate/2));
status->changeItem (buf,2);
if (data [(int)(x*this->x)])
{
double rea= data [col][(int)(y*points/2)].real;
double ima= data [col][(int)(y*points/2)].imag;
sprintf (buf,"Amplitude: %d %%",(int)(sqrt(rea*rea+ima*ima)/max*100));
}
else sprintf (buf,"Memory Leak !");
status->changeItem (buf,3);
if (data [(int)(x*this->x)])
{
double rea= data [col][(int)(y*points/2)].real;
double ima= data [col][(int)(y*points/2)].imag;
sprintf (buf,"Phase: %d degree",(int) (atan(ima/rea)*360/M_PI));
}
else sprintf (buf,"Memory Leak !");
status->changeItem (buf,4);
}
//****************************************************************************
void SonagramWindow::setRange (int offset,int width,int)
{
xscale->setMaxMin ((int)(((double)offset+width)*points*1000/rate),
(int)(((double)offset)*points*1000/rate));
}