5 from scipy import fftpack
7 print "Please install SciPy to run this script (http://www.scipy.org/)"
11 from PyQt4 import Qt, QtCore, QtGui
12 import PyQt4.Qwt5 as Qwt
13 from optparse import OptionParser
14 from gnuradio import gr, blks2, eng_notation
15 from scipy import fftpack
17 from pyqt_filter import Ui_MainWindow
19 class gr_plot_filter(QtGui.QMainWindow):
20 def __init__(self, qapp, options):
21 QtGui.QWidget.__init__(self, None)
22 self.gui = Ui_MainWindow()
23 self.gui.setupUi(self)
25 self.connect(self.gui.filterTypeComboBox,
26 Qt.SIGNAL("currentIndexChanged(const QString&)"),
27 self.changed_filter_type)
28 self.connect(self.gui.filterDesignTypeComboBox,
29 Qt.SIGNAL("currentIndexChanged(const QString&)"),
30 self.changed_filter_design_type)
32 self.connect(self.gui.designButton,
33 Qt.SIGNAL("released()"),
36 self.connect(self.gui.tabGroup,
37 Qt.SIGNAL("currentChanged(int)"),
40 self.connect(self.gui.nfftEdit,
41 Qt.SIGNAL("textEdited(QString)"),
42 self.nfft_edit_changed)
44 self.gui.designButton.setShortcut(QtCore.Qt.Key_Return)
50 self.nfftpts = int(10000)
51 self.gui.nfftEdit.setText(Qt.QString("%1").arg(self.nfftpts))
53 self.gui.lpfPassBandRippleLabel.setVisible(False)
54 self.gui.lpfPassBandRippleEdit.setVisible(False)
55 self.gui.bpfPassBandRippleLabel.setVisible(False)
56 self.gui.bpfPassBandRippleEdit.setVisible(False)
57 self.gui.bnfPassBandRippleLabel.setVisible(False)
58 self.gui.bnfPassBandRippleEdit.setVisible(False)
59 self.gui.hpfPassBandRippleLabel.setVisible(False)
60 self.gui.hpfPassBandRippleEdit.setVisible(False)
63 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firlpfPage)
66 self.gui.freqPlot.setAxisTitle(self.gui.freqPlot.xBottom,
68 self.gui.freqPlot.setAxisTitle(self.gui.freqPlot.yLeft,
70 self.gui.timePlot.setAxisTitle(self.gui.timePlot.xBottom,
72 self.gui.timePlot.setAxisTitle(self.gui.timePlot.yLeft,
74 self.gui.phasePlot.setAxisTitle(self.gui.phasePlot.xBottom,
76 self.gui.phasePlot.setAxisTitle(self.gui.phasePlot.yLeft,
78 self.gui.groupPlot.setAxisTitle(self.gui.groupPlot.xBottom,
80 self.gui.groupPlot.setAxisTitle(self.gui.groupPlot.yLeft,
84 self.rcurve = Qwt.QwtPlotCurve("Real")
85 self.rcurve.attach(self.gui.timePlot)
86 self.icurve = Qwt.QwtPlotCurve("Imag")
87 self.icurve.attach(self.gui.timePlot)
89 self.freqcurve = Qwt.QwtPlotCurve("PSD")
90 self.freqcurve.attach(self.gui.freqPlot)
92 self.phasecurve = Qwt.QwtPlotCurve("Phase")
93 self.phasecurve.attach(self.gui.phasePlot)
95 self.groupcurve = Qwt.QwtPlotCurve("Group Delay")
96 self.groupcurve.attach(self.gui.groupPlot)
98 # Create zoom functionality for the plots
99 self.timeZoomer = Qwt.QwtPlotZoomer(self.gui.timePlot.xBottom,
100 self.gui.timePlot.yLeft,
101 Qwt.QwtPicker.PointSelection,
102 Qwt.QwtPicker.AlwaysOn,
103 self.gui.timePlot.canvas())
105 self.freqZoomer = Qwt.QwtPlotZoomer(self.gui.freqPlot.xBottom,
106 self.gui.freqPlot.yLeft,
107 Qwt.QwtPicker.PointSelection,
108 Qwt.QwtPicker.AlwaysOn,
109 self.gui.freqPlot.canvas())
111 self.phaseZoomer = Qwt.QwtPlotZoomer(self.gui.phasePlot.xBottom,
112 self.gui.phasePlot.yLeft,
113 Qwt.QwtPicker.PointSelection,
114 Qwt.QwtPicker.AlwaysOn,
115 self.gui.phasePlot.canvas())
117 self.groupZoomer = Qwt.QwtPlotZoomer(self.gui.groupPlot.xBottom,
118 self.gui.groupPlot.yLeft,
119 Qwt.QwtPicker.PointSelection,
120 Qwt.QwtPicker.AlwaysOn,
121 self.gui.groupPlot.canvas())
123 # Set up pen for colors and line width
124 blue = QtGui.qRgb(0x00, 0x00, 0xFF)
125 blueBrush = Qt.QBrush(Qt.QColor(blue))
126 self.freqcurve.setPen(Qt.QPen(blueBrush, 2))
127 self.rcurve.setPen(Qt.QPen(blueBrush, 2))
128 self.phasecurve.setPen(Qt.QPen(blueBrush, 2))
129 self.groupcurve.setPen(Qt.QPen(blueBrush, 2))
132 self.gui.nTapsEdit.setText("0")
134 self.filterWindows = {"Hamming Window" : gr.firdes.WIN_HAMMING,
135 "Hann Window" : gr.firdes.WIN_HANN,
136 "Blackman Window" : gr.firdes.WIN_BLACKMAN,
137 "Rectangular Window" : gr.firdes.WIN_RECTANGULAR,
138 "Kaiser Window" : gr.firdes.WIN_KAISER,
139 "Blackman-harris Window" : gr.firdes.WIN_BLACKMAN_hARRIS}
143 def changed_filter_type(self, ftype):
144 strftype = str(ftype.toAscii())
145 if(ftype == "Low Pass"):
146 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firlpfPage)
147 elif(ftype == "Band Pass"):
148 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbpfPage)
149 elif(ftype == "Complex Band Pass"):
150 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbpfPage)
151 elif(ftype == "Band Notch"):
152 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbnfPage)
153 elif(ftype == "High Pass"):
154 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firhpfPage)
155 elif(ftype == "Root Raised Cosine"):
156 self.gui.filterTypeWidget.setCurrentWidget(self.gui.rrcPage)
157 elif(ftype == "Gaussian"):
158 self.gui.filterTypeWidget.setCurrentWidget(self.gui.gausPage)
162 def changed_filter_design_type(self, design):
163 if(design == "Equiripple"):
164 self.set_equiripple()
170 def set_equiripple(self):
171 self.equiripple = True
172 self.gui.lpfPassBandRippleLabel.setVisible(True)
173 self.gui.lpfPassBandRippleEdit.setVisible(True)
174 self.gui.bpfPassBandRippleLabel.setVisible(True)
175 self.gui.bpfPassBandRippleEdit.setVisible(True)
176 self.gui.bnfPassBandRippleLabel.setVisible(True)
177 self.gui.bnfPassBandRippleEdit.setVisible(True)
178 self.gui.hpfPassBandRippleLabel.setVisible(True)
179 self.gui.hpfPassBandRippleEdit.setVisible(True)
181 def set_windowed(self):
182 self.equiripple = False
183 self.gui.lpfPassBandRippleLabel.setVisible(False)
184 self.gui.lpfPassBandRippleEdit.setVisible(False)
185 self.gui.bpfPassBandRippleLabel.setVisible(False)
186 self.gui.bpfPassBandRippleEdit.setVisible(False)
187 self.gui.bnfPassBandRippleLabel.setVisible(False)
188 self.gui.bnfPassBandRippleEdit.setVisible(False)
189 self.gui.hpfPassBandRippleLabel.setVisible(False)
190 self.gui.hpfPassBandRippleEdit.setVisible(False)
194 fs,r = self.gui.sampleRateEdit.text().toDouble()
196 gain,r = self.gui.filterGainEdit.text().toDouble()
200 winstr = str(self.gui.filterDesignTypeComboBox.currentText().toAscii())
201 ftype = str(self.gui.filterTypeComboBox.currentText().toAscii())
203 if(winstr == "Equiripple"):
204 designer = {"Low Pass" : self.design_opt_lpf,
205 "Band Pass" : self.design_opt_bpf,
206 "Complex Band Pass" : self.design_opt_cbpf,
207 "Band Notch" : self.design_opt_bnf,
208 "High Pass" : self.design_opt_hpf}
209 taps,r = designer[ftype](fs, gain)
212 designer = {"Low Pass" : self.design_win_lpf,
213 "Band Pass" : self.design_win_bpf,
214 "Complex Band Pass" : self.design_win_cbpf,
215 "Band Notch" : self.design_win_bnf,
216 "High Pass" : self.design_win_hpf,
217 "Root Raised Cosine" : self.design_win_rrc,
218 "Gaussian" : self.design_win_gaus}
219 wintype = self.filterWindows[winstr]
220 taps,r = designer[ftype](fs, gain, wintype)
223 self.taps = scipy.array(taps)
224 self.get_fft(fs, self.taps, self.nfftpts)
225 self.update_time_curves()
226 self.update_freq_curves()
227 self.update_phase_curves()
228 self.update_group_curves()
230 self.gui.nTapsEdit.setText(Qt.QString("%1").arg(self.taps.size))
233 # Filter design functions using a window
234 def design_win_lpf(self, fs, gain, wintype):
236 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
238 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
240 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
246 taps = gr.firdes.low_pass_2(gain, fs, pb, tb,
252 def design_win_bpf(self, fs, gain, wintype):
254 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
256 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
258 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
260 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
264 taps = gr.firdes.band_pass_2(gain, fs, pb1, pb2, tb,
270 def design_win_cbpf(self, fs, gain, wintype):
272 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
274 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
276 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
278 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
282 taps = gr.firdes.complex_band_pass_2(gain, fs, pb1, pb2, tb,
288 def design_win_bnf(self, fs, gain, wintype):
290 pb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
292 pb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
294 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
296 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
300 taps = gr.firdes.band_reject_2(gain, fs, pb1, pb2, tb,
306 def design_win_hpf(self, fs, gain, wintype):
308 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
310 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
312 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
317 taps = gr.firdes.high_pass_2(gain, fs, pb, tb,
323 def design_win_rrc(self, fs, gain, wintype):
325 sr,r = self.gui.rrcSymbolRateEdit.text().toDouble()
327 alpha,r = self.gui.rrcAlphaEdit.text().toDouble()
329 ntaps,r = self.gui.rrcNumTapsEdit.text().toInt()
333 taps = gr.firdes.root_raised_cosine(gain, fs, sr,
339 def design_win_gaus(self, fs, gain, wintype):
341 sr,r = self.gui.gausSymbolRateEdit.text().toDouble()
343 bt,r = self.gui.gausBTEdit.text().toDouble()
345 ntaps,r = self.gui.gausNumTapsEdit.text().toInt()
350 taps = gr.firdes.gaussian(gain, spb, bt, ntaps)
355 # Design Functions for Equiripple Filters
356 def design_opt_lpf(self, fs, gain):
358 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
360 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
362 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
364 ripple,r = self.gui.lpfPassBandRippleEdit.text().toDouble()
368 taps = blks2.optfir.low_pass(gain, fs, pb, sb,
374 def design_opt_bpf(self, fs, gain):
376 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
378 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
380 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
382 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
384 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
390 taps = blks2.optfir.band_pass(gain, fs, sb1, pb1, pb2, sb2,
396 def design_opt_cbpf(self, fs, gain):
398 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
400 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
402 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
404 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
406 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
412 taps = blks2.optfir.complex_band_pass(gain, fs, sb1, pb1, pb2, sb2,
418 def design_opt_bnf(self, fs, gain):
420 sb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
422 sb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
424 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
426 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
428 ripple,r = self.gui.bnfPassBandRippleEdit.text().toDouble()
434 taps = blks2.optfir.band_reject(gain, fs, pb1, sb1, sb2, pb2,
440 def design_opt_hpf(self, fs, gain):
442 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
444 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
446 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
448 ripple,r = self.gui.hpfPassBandRippleEdit.text().toDouble()
452 taps = blks2.optfir.high_pass(gain, fs, sb, pb,
458 def nfft_edit_changed(self, nfft):
459 infft,r = nfft.toInt()
460 if(r and (infft != self.nfftpts)):
462 self.update_freq_curves()
464 def tab_changed(self, tab):
466 self.update_freq_curves()
468 self.update_time_curves()
470 self.update_phase_curves()
472 self.update_group_curves()
474 def get_fft(self, fs, taps, Npts):
476 fftpts = fftpack.fft(taps, Npts)
477 self.freq = scipy.arange(0, fs, 1.0/(Npts*Ts))
478 self.fftdB = 20.0*scipy.log10(abs(fftpts))
479 self.fftDeg = scipy.unwrap(scipy.angle(fftpts))
480 self.groupDelay = -scipy.diff(self.fftDeg)
482 def update_time_curves(self):
483 ntaps = len(self.taps)
485 if(type(self.taps[0]) == scipy.complex128):
486 self.rcurve.setData(scipy.arange(ntaps), self.taps.real)
487 self.icurve.setData(scipy.arange(ntaps), self.taps.imag)
489 self.rcurve.setData(scipy.arange(ntaps), self.taps)
491 # Reset the x-axis to the new time scale
492 ymax = 1.5 * max(self.taps)
493 ymin = 1.5 * min(self.taps)
494 self.gui.timePlot.setAxisScale(self.gui.timePlot.xBottom,
496 self.gui.timePlot.setAxisScale(self.gui.timePlot.yLeft,
499 # Set the zoomer base to unzoom to the new axis
500 self.timeZoomer.setZoomBase()
502 self.gui.timePlot.replot()
504 def update_freq_curves(self):
505 npts = len(self.fftdB)
507 self.freqcurve.setData(self.freq, self.fftdB)
509 # Reset the x-axis to the new time scale
510 ymax = 1.5 * max(self.fftdB[0:npts/2])
511 ymin = 1.1 * min(self.fftdB[0:npts/2])
512 xmax = self.freq[npts/2]
514 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.xBottom,
516 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.yLeft,
519 # Set the zoomer base to unzoom to the new axis
520 self.freqZoomer.setZoomBase()
522 self.gui.freqPlot.replot()
525 def update_phase_curves(self):
526 npts = len(self.fftDeg)
528 self.phasecurve.setData(self.freq, self.fftDeg)
530 # Reset the x-axis to the new time scale
531 ymax = 1.5 * max(self.fftDeg[0:npts/2])
532 ymin = 1.1 * min(self.fftDeg[0:npts/2])
533 xmax = self.freq[npts/2]
535 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.xBottom,
537 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.yLeft,
540 # Set the zoomer base to unzoom to the new axis
541 self.phaseZoomer.setZoomBase()
543 self.gui.phasePlot.replot()
545 def update_group_curves(self):
546 npts = len(self.groupDelay)
548 self.groupcurve.setData(self.freq, self.groupDelay)
550 # Reset the x-axis to the new time scale
551 ymax = 1.5 * max(self.groupDelay[0:npts/2])
552 ymin = 1.1 * min(self.groupDelay[0:npts/2])
553 xmax = self.freq[npts/2]
555 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.xBottom,
557 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.yLeft,
560 # Set the zoomer base to unzoom to the new axis
561 self.groupZoomer.setZoomBase()
563 self.gui.groupPlot.replot()
567 usage="%prog: [options] (input_filename)"
570 parser = OptionParser(conflict_handler="resolve",
571 usage=usage, description=description)
575 parser = setup_options()
576 (options, args) = parser.parse_args ()
578 app = Qt.QApplication(args)
579 gplt = gr_plot_filter(app, options)
582 if __name__ == '__main__':