kwave  18.07.70
FrequencyResponseWidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  FrequencyResponseWidget.cpp - displays a frequency response
3  -------------------
4  begin : Mar 09 2003
5  copyright : (C) 2003 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <math.h>
21 #include <stdlib.h>
22 
23 #include <QBrush>
24 #include <QPainter>
25 #include <QPixmap>
26 
28 #include "libkwave/Utils.h"
29 
31 
32 //***************************************************************************
34  :QWidget(widget), m_f_max(0), m_db_min(0), m_db_max(0),
35  m_decades(0), m_function(Q_NULLPTR)
36 {
37  init(10000, -12, +12);
38 }
39 
40 //***************************************************************************
42 {
43 }
44 
45 //***************************************************************************
46 void Kwave::FrequencyResponseWidget::init(double freq, int db_min, int db_max)
47 {
48  const int base = 10;
49  m_decades = Kwave::toInt(ceil(log(freq)/log(base)));
50  m_f_max = pow(base, m_decades);
51 
52  m_db_min = db_min;
53  m_db_max = db_max;
54 }
55 
56 //***************************************************************************
59 {
60  m_function = func;
61  repaint();
62 }
63 
64 //***************************************************************************
66 {
67 // const int base = 10;
68 // const double m_frequency = m_f_max * 2/3;
69  const int width = this->width();
70  const int height = this->height();
71 
72  Q_ASSERT(width > 0);
73  Q_ASSERT(height > 0);
74  if ((width <= 0) || (height <= 0)) return;
75 
76  QPainter p(this);
77  p.fillRect(rect(), QBrush(palette().dark()));
78 
79  double scale = static_cast<double>(height-1) /
80  static_cast<double>(m_db_max-m_db_min);
81  double min = pow(10.0, static_cast<double>(m_db_min) / 10.0);
82  double max = pow(10.0, static_cast<double>(m_db_max) / 10.0);
83  p.setPen(Qt::green);//colorGroup().text());
84 
85  for (int x=0; x < width; x++) {
86  // transform x coordinate to frequency
87 
88 // // logarithmic frequency scale, didn't look so good :-(
89 // double f = pow(base, (double)m_decades * (double)x / (double)width);
90 
91  // linear frequency scale
92  double f = (m_f_max * static_cast<double>(x) /
93  static_cast<double>(width));
94 
95  // calculate the filter function's output at the given frequency
96  f = (f / m_f_max) * M_PI;
97  double a = (m_function) ? m_function->at(f): 1.0;
98 
99  // limit to upper and lower margins
100  if (a < min) a = min;
101  if (a > max) a = max;
102 
103  // convert to logarithmic scale
104  double db = 10.0 * log10(a);
105 
106  // draw one line
107  int y = height - Kwave::toInt((db - m_db_min) * scale);
108 
109  p.drawLine(x, y+1, x, height-1);
110  }
111 
112  // draw the zero db line
113  p.setPen(palette().text().color());
114  int y = height - Kwave::toInt((0.0 - m_db_min) * scale);
115  p.drawLine(0, y, width-1, y);
116 }
117 
118 //***************************************************************************
119 //***************************************************************************
virtual void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE
virtual void init(double freq, int db_min, int db_max)
Kwave::TransmissionFunction * m_function
int toInt(T x)
Definition: Utils.h:127
virtual ~FrequencyResponseWidget() Q_DECL_OVERRIDE
virtual double at(double f)=0
virtual void setFilter(Kwave::TransmissionFunction *func)
static double rect(double param)
Definition: Functions.cpp:29