Skip to content

Commit 618836e

Browse files
author
Tony Kirke
committed
Add Flattop to window functions
1 parent 342ff03 commit 618836e

File tree

8 files changed

+47
-1
lines changed

8 files changed

+47
-1
lines changed

qt_window/mainwindow.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ MainWindow::MainWindow(QWidget *parent) :
2828
Blackman_on = NULL;
2929
Bartlett_on = NULL;
3030
Chebyshev_on = NULL;
31+
Flattop_on = NULL;
3132
Kaiser_on = NULL;
3233

3334
graph_counter = 0;
@@ -44,6 +45,7 @@ MainWindow::MainWindow(QWidget *parent) :
4445
connect(ui->Blackman, SIGNAL(released()), this, SLOT(EChanged()));
4546
connect(ui->Bartlett, SIGNAL(released()), this, SLOT(FHChanged()));
4647
connect(ui->Chebyshev, SIGNAL(released()), this, SLOT(GChanged()));
48+
connect(ui->Flattop, SIGNAL(released()), this, SLOT(FTChanged()));
4749
connect(ui->Kaiser, SIGNAL(released()), this, SLOT(CBChanged()));
4850

4951
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)),
@@ -57,7 +59,7 @@ MainWindow::MainWindow(QWidget *parent) :
5759
void MainWindow::BChanged() {
5860
if (Hanning_on==NULL) {
5961
shape = "Hanning";
60-
LPF.change_filter(spuce::Hamming);
62+
LPF.change_filter(spuce::Hanning);
6163
Hanning_on = ui->customPlot->addGraph();
6264
plot2(ui->customPlot);
6365
} else {
@@ -114,6 +116,18 @@ void MainWindow::GChanged() {
114116
ui->customPlot->replot();
115117
}
116118
}
119+
void MainWindow::FTChanged() {
120+
if (Flattop_on==NULL) {
121+
shape = "Flattop";
122+
LPF.change_filter(spuce::Flattop);
123+
Flattop_on = ui->customPlot->addGraph();
124+
plot2(ui->customPlot);
125+
} else {
126+
ui->customPlot->removeGraph(Flattop_on);
127+
Flattop_on = NULL;
128+
ui->customPlot->replot();
129+
}
130+
}
117131
void MainWindow::CBChanged() {
118132
if (Kaiser_on==NULL) {
119133
shape = "Kaiser";
@@ -133,6 +147,7 @@ QCPGraph* MainWindow::GetPtr() {
133147
else if (shape=="Hanning") return(Hanning_on);
134148
else if (shape=="Blackman") return(Blackman_on);
135149
else if (shape=="Chebyshev") return(Chebyshev_on);
150+
else if (shape=="Flattop") return(Flattop_on);
136151
else if (shape=="Kaiser") return(Kaiser_on);
137152
else std::cout << "Invalid filter selection " << shape << "\n";
138153
return(Hanning_on);

qt_window/mainwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public slots:
3333
void GChanged();
3434
void EChanged();
3535
void FHChanged();
36+
void FTChanged();
3637
void CBChanged();
3738
void graphPressEvent(QMouseEvent *event);
3839
void graphMoveEvent(QMouseEvent *event);
@@ -51,6 +52,7 @@ public slots:
5152
QCPGraph* Blackman_on;
5253
QCPGraph* Bartlett_on;
5354
QCPGraph* Chebyshev_on;
55+
QCPGraph* Flattop_on;
5456
QCPGraph* Kaiser_on;
5557
int graph_counter;
5658

qt_window/make_filter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void make_filter::reset() {
3636
hamming_taps =23;
3737
hanning_taps =23;
3838
bartlett_taps =23;
39+
flattop_taps =23;
3940
blackman_taps =23;
4041
kaiser_taps =23;
4142
cheby_taps =23;
@@ -63,6 +64,7 @@ void make_filter::vertical_swipe(int len) {
6364
case Hamming:
6465
case Blackman:
6566
case Bartlett:
67+
case Flattop:
6668
case None:
6769
break;
6870
}
@@ -76,6 +78,7 @@ void make_filter::horiz_swipe(int len) {
7678
case Hanning: hanning_taps = limit(hanning_taps + inc, MAX_FIR,MIN_FIR); break;
7779
case Hamming: hamming_taps = limit(hamming_taps + inc, MAX_FIR,MIN_FIR); break;
7880
case Blackman: blackman_taps = limit(blackman_taps + inc, MAX_FIR,MIN_FIR); break;
81+
case Flattop: flattop_taps = limit(flattop_taps + inc, MAX_FIR,MIN_FIR); break;
7982
case Bartlett: bartlett_taps = limit(bartlett_taps + inc, MAX_FIR,MIN_FIR); break;
8083
case Chebyshev: cheby_taps = limit(cheby_taps + inc, MAX_FIR, MIN_FIR); break;
8184
case Kaiser: kaiser_taps = limit(kaiser_taps + inc, MAX_FIR, MIN_FIR); break;
@@ -91,8 +94,10 @@ double make_filter::update(double *w) {
9194
case Blackman: taps = design_window("blackman", blackman_taps); break;
9295
case Bartlett: taps = design_window("bartlett", bartlett_taps); break;
9396
case Kaiser: taps = design_window("kaiser", kaiser_taps, kaiser_beta); break;
97+
case Flattop: taps = design_window("flattop", flattop_taps); break;
9498
case None: for (int i = 0; i < pts; i++) w[i] = 1.0; break;
9599
}
100+
96101
// Normalize for Frequency plots
97102
float_type sum = 0;
98103
for (size_t i=0;i<taps.size();i++) sum += taps[i];

qt_window/make_filter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum fil_enum {
1111
Bartlett,
1212
Blackman,
1313
Kaiser,
14+
Flattop,
1415
Chebyshev
1516
};
1617

@@ -26,6 +27,7 @@ class make_filter {
2627
int hamming_taps;
2728
int hanning_taps;
2829
int bartlett_taps;
30+
int flattop_taps;
2931
int blackman_taps;
3032
int kaiser_taps;
3133

qt_window/ui_mainwindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Ui_MainWindow
3434
QPushButton *Kaiser;
3535
QPushButton *Bartlett;
3636
QPushButton *Chebyshev;
37+
QPushButton *Flattop;
3738
QPushButton *Hanning;
3839
QStatusBar *statusBar;
3940

@@ -85,6 +86,10 @@ class Ui_MainWindow
8586
Chebyshev->setObjectName(QStringLiteral("Chebyshev"));
8687
Chebyshev->setGeometry(QRect(10, 190, 181, 27));
8788
Chebyshev->setCheckable(true);
89+
Flattop = new QPushButton(centralWidget);
90+
Flattop->setObjectName(QStringLiteral("Flattop"));
91+
Flattop->setGeometry(QRect(10, 220, 181, 27));
92+
Flattop->setCheckable(true);
8893
Hanning = new QPushButton(centralWidget);
8994
Hanning->setObjectName(QStringLiteral("Hanning"));
9095
Hanning->setGeometry(QRect(10, 40, 181, 27));
@@ -108,6 +113,7 @@ class Ui_MainWindow
108113
Kaiser->setText(QApplication::translate("MainWindow", "Kaiser", 0));
109114
Bartlett->setText(QApplication::translate("MainWindow", "Bartlett", 0));
110115
Chebyshev->setText(QApplication::translate("MainWindow", "Chebyshev", 0));
116+
Flattop->setText(QApplication::translate("MainWindow", "Flattop", 0));
111117
Hanning->setText(QApplication::translate("MainWindow", "Hanning", 0));
112118
} // retranslateUi
113119

spuce/filters/design_window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ std::vector<float_type> design_window(const std::string& fir_type, int order, fl
2323
win = bartlett(order);
2424
} else if (fir_type == "chebyshev") {
2525
win = cheby(order, beta);
26+
} else if (fir_type == "flattop") {
27+
win = flattop(order);
2628
} else {
2729
win = rectangular(order);
2830
}

spuce/filters/window.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ std::vector<float_type> rectangular(long nf) {
7676
for (int i = 0; i < nf; i++) w[i] = 1.0;
7777
return (w);
7878
}
79+
std::vector<float_type> flattop(long nf)
80+
{
81+
static const double a0 = 1.0, a1 = 1.93, a2 = 1.29, a3 = 0.388, a4=0.028;
82+
std::vector<float_type> w(nf);
83+
for (int i = 0; i < nf; i++) {
84+
w[i] = a0 -a1*std::cos((2.0*M_PI*i)/(nf-1))
85+
+a2*std::cos((4.0*M_PI*i)/(nf-1))
86+
-a3*std::cos((6.0*M_PI*i)/(nf-1))
87+
+a4*std::cos((8.0*M_PI*i)/(nf-1));
88+
}
89+
return w;
90+
}
7991
//! \ingroup fir
8092
//! \brief hamming window \f$ w(n) = 0.54 - 0.46*cos( 2*\pi*n/(nf-1) )\f$
8193
std::vector<float_type> hamming(long nf) {

spuce/filters/window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace spuce {
99
//! \brief rectangular window \f$ w(n) = 1;\f$
1010
//! \ingroup functions fir
1111
std::vector<float_type> rectangular(long nf);
12+
//! \ingroup functions fir
13+
std::vector<float_type> flattop(long nf);
1214
//! \brief hamming window \f$ w(n) = 0.54 - 0.46*cos( 2*\pi*n/(nf-1) )\f$
1315
//! \ingroup functions fir
1416
std::vector<float_type> hamming(long nf);

0 commit comments

Comments
 (0)