4 from optparse import OptionParser
5 from gnuradio import gr, blks2, eng_notation
9 from scipy import fftpack
11 print "Please install SciPy to run this script (http://www.scipy.org/)"
15 from PyQt4 import Qt, QtCore, QtGui
17 print "Please install PyQt4 to run this script (http://www.riverbankcomputing.co.uk/software/pyqt/download)"
21 import PyQt4.Qwt5 as Qwt
23 print "Please install PyQwt5 to run this script (http://pyqwt.sourceforge.net/)"
27 from pyqt_filter import Ui_MainWindow
29 print "Could not import from pyqt_filter. Please build with \"pyuic4 pyqt_filter.ui -o pyqt_filter.py\""
33 class gr_plot_filter(QtGui.QMainWindow):
34 def __init__(self, qapp, options):
35 QtGui.QWidget.__init__(self, None)
36 self.gui = Ui_MainWindow()
37 self.gui.setupUi(self)
39 self.connect(self.gui.filterTypeComboBox,
40 Qt.SIGNAL("currentIndexChanged(const QString&)"),
41 self.changed_filter_type)
42 self.connect(self.gui.filterDesignTypeComboBox,
43 Qt.SIGNAL("currentIndexChanged(const QString&)"),
44 self.changed_filter_design_type)
46 self.connect(self.gui.designButton,
47 Qt.SIGNAL("released()"),
50 self.connect(self.gui.tabGroup,
51 Qt.SIGNAL("currentChanged(int)"),
54 self.connect(self.gui.nfftEdit,
55 Qt.SIGNAL("textEdited(QString)"),
56 self.nfft_edit_changed)
58 self.gui.designButton.setShortcut(QtCore.Qt.Key_Return)
64 self.nfftpts = int(10000)
65 self.gui.nfftEdit.setText(Qt.QString("%1").arg(self.nfftpts))
67 self.gui.lpfPassBandRippleLabel.setVisible(False)
68 self.gui.lpfPassBandRippleEdit.setVisible(False)
69 self.gui.bpfPassBandRippleLabel.setVisible(False)
70 self.gui.bpfPassBandRippleEdit.setVisible(False)
71 self.gui.bnfPassBandRippleLabel.setVisible(False)
72 self.gui.bnfPassBandRippleEdit.setVisible(False)
73 self.gui.hpfPassBandRippleLabel.setVisible(False)
74 self.gui.hpfPassBandRippleEdit.setVisible(False)
77 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firlpfPage)
80 self.gui.freqPlot.setAxisTitle(self.gui.freqPlot.xBottom,
82 self.gui.freqPlot.setAxisTitle(self.gui.freqPlot.yLeft,
84 self.gui.timePlot.setAxisTitle(self.gui.timePlot.xBottom,
86 self.gui.timePlot.setAxisTitle(self.gui.timePlot.yLeft,
88 self.gui.phasePlot.setAxisTitle(self.gui.phasePlot.xBottom,
90 self.gui.phasePlot.setAxisTitle(self.gui.phasePlot.yLeft,
92 self.gui.groupPlot.setAxisTitle(self.gui.groupPlot.xBottom,
94 self.gui.groupPlot.setAxisTitle(self.gui.groupPlot.yLeft,
98 self.rcurve = Qwt.QwtPlotCurve("Real")
99 self.rcurve.attach(self.gui.timePlot)
100 self.icurve = Qwt.QwtPlotCurve("Imag")
101 self.icurve.attach(self.gui.timePlot)
103 self.freqcurve = Qwt.QwtPlotCurve("PSD")
104 self.freqcurve.attach(self.gui.freqPlot)
106 self.phasecurve = Qwt.QwtPlotCurve("Phase")
107 self.phasecurve.attach(self.gui.phasePlot)
109 self.groupcurve = Qwt.QwtPlotCurve("Group Delay")
110 self.groupcurve.attach(self.gui.groupPlot)
112 # Create zoom functionality for the plots
113 self.timeZoomer = Qwt.QwtPlotZoomer(self.gui.timePlot.xBottom,
114 self.gui.timePlot.yLeft,
115 Qwt.QwtPicker.PointSelection,
116 Qwt.QwtPicker.AlwaysOn,
117 self.gui.timePlot.canvas())
119 self.freqZoomer = Qwt.QwtPlotZoomer(self.gui.freqPlot.xBottom,
120 self.gui.freqPlot.yLeft,
121 Qwt.QwtPicker.PointSelection,
122 Qwt.QwtPicker.AlwaysOn,
123 self.gui.freqPlot.canvas())
125 self.phaseZoomer = Qwt.QwtPlotZoomer(self.gui.phasePlot.xBottom,
126 self.gui.phasePlot.yLeft,
127 Qwt.QwtPicker.PointSelection,
128 Qwt.QwtPicker.AlwaysOn,
129 self.gui.phasePlot.canvas())
131 self.groupZoomer = Qwt.QwtPlotZoomer(self.gui.groupPlot.xBottom,
132 self.gui.groupPlot.yLeft,
133 Qwt.QwtPicker.PointSelection,
134 Qwt.QwtPicker.AlwaysOn,
135 self.gui.groupPlot.canvas())
137 # Set up pen for colors and line width
138 blue = QtGui.qRgb(0x00, 0x00, 0xFF)
139 blueBrush = Qt.QBrush(Qt.QColor(blue))
140 self.freqcurve.setPen(Qt.QPen(blueBrush, 2))
141 self.rcurve.setPen(Qt.QPen(blueBrush, 2))
142 self.phasecurve.setPen(Qt.QPen(blueBrush, 2))
143 self.groupcurve.setPen(Qt.QPen(blueBrush, 2))
145 # Set up validators for edit boxes
146 self.intVal = Qt.QIntValidator(None)
147 self.dblVal = Qt.QDoubleValidator(None)
148 self.gui.nfftEdit.setValidator(self.intVal)
149 self.gui.sampleRateEdit.setValidator(self.dblVal)
150 self.gui.filterGainEdit.setValidator(self.dblVal)
151 self.gui.endofLpfPassBandEdit.setValidator(self.dblVal)
152 self.gui.startofLpfStopBandEdit.setValidator(self.dblVal)
153 self.gui.lpfStopBandAttenEdit.setValidator(self.dblVal)
154 self.gui.lpfPassBandRippleEdit.setValidator(self.dblVal)
155 self.gui.startofBpfPassBandEdit.setValidator(self.dblVal)
156 self.gui.endofBpfPassBandEdit.setValidator(self.dblVal)
157 self.gui.bpfTransitionEdit.setValidator(self.dblVal)
158 self.gui.bpfStopBandAttenEdit.setValidator(self.dblVal)
159 self.gui.bpfPassBandRippleEdit.setValidator(self.dblVal)
160 self.gui.startofBnfStopBandEdit.setValidator(self.dblVal)
161 self.gui.endofBnfStopBandEdit.setValidator(self.dblVal)
162 self.gui.bnfTransitionEdit.setValidator(self.dblVal)
163 self.gui.bnfStopBandAttenEdit.setValidator(self.dblVal)
164 self.gui.bnfPassBandRippleEdit.setValidator(self.dblVal)
165 self.gui.endofHpfStopBandEdit.setValidator(self.dblVal)
166 self.gui.startofHpfPassBandEdit.setValidator(self.dblVal)
167 self.gui.hpfStopBandAttenEdit.setValidator(self.dblVal)
168 self.gui.hpfPassBandRippleEdit.setValidator(self.dblVal)
169 self.gui.rrcSymbolRateEdit.setValidator(self.dblVal)
170 self.gui.rrcAlphaEdit.setValidator(self.dblVal)
171 self.gui.rrcNumTapsEdit.setValidator(self.dblVal)
172 self.gui.gausSymbolRateEdit.setValidator(self.dblVal)
173 self.gui.gausBTEdit.setValidator(self.dblVal)
174 self.gui.gausNumTapsEdit.setValidator(self.dblVal)
176 self.gui.nTapsEdit.setText("0")
178 self.filterWindows = {"Hamming Window" : gr.firdes.WIN_HAMMING,
179 "Hann Window" : gr.firdes.WIN_HANN,
180 "Blackman Window" : gr.firdes.WIN_BLACKMAN,
181 "Rectangular Window" : gr.firdes.WIN_RECTANGULAR,
182 "Kaiser Window" : gr.firdes.WIN_KAISER,
183 "Blackman-harris Window" : gr.firdes.WIN_BLACKMAN_hARRIS}
187 def changed_filter_type(self, ftype):
188 strftype = str(ftype.toAscii())
189 if(ftype == "Low Pass"):
190 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firlpfPage)
191 elif(ftype == "Band Pass"):
192 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbpfPage)
193 elif(ftype == "Complex Band Pass"):
194 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbpfPage)
195 elif(ftype == "Band Notch"):
196 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbnfPage)
197 elif(ftype == "High Pass"):
198 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firhpfPage)
199 elif(ftype == "Root Raised Cosine"):
200 self.gui.filterTypeWidget.setCurrentWidget(self.gui.rrcPage)
201 elif(ftype == "Gaussian"):
202 self.gui.filterTypeWidget.setCurrentWidget(self.gui.gausPage)
206 def changed_filter_design_type(self, design):
207 if(design == "Equiripple"):
208 self.set_equiripple()
214 def set_equiripple(self):
215 self.equiripple = True
216 self.gui.lpfPassBandRippleLabel.setVisible(True)
217 self.gui.lpfPassBandRippleEdit.setVisible(True)
218 self.gui.bpfPassBandRippleLabel.setVisible(True)
219 self.gui.bpfPassBandRippleEdit.setVisible(True)
220 self.gui.bnfPassBandRippleLabel.setVisible(True)
221 self.gui.bnfPassBandRippleEdit.setVisible(True)
222 self.gui.hpfPassBandRippleLabel.setVisible(True)
223 self.gui.hpfPassBandRippleEdit.setVisible(True)
225 def set_windowed(self):
226 self.equiripple = False
227 self.gui.lpfPassBandRippleLabel.setVisible(False)
228 self.gui.lpfPassBandRippleEdit.setVisible(False)
229 self.gui.bpfPassBandRippleLabel.setVisible(False)
230 self.gui.bpfPassBandRippleEdit.setVisible(False)
231 self.gui.bnfPassBandRippleLabel.setVisible(False)
232 self.gui.bnfPassBandRippleEdit.setVisible(False)
233 self.gui.hpfPassBandRippleLabel.setVisible(False)
234 self.gui.hpfPassBandRippleEdit.setVisible(False)
238 fs,r = self.gui.sampleRateEdit.text().toDouble()
240 gain,r = self.gui.filterGainEdit.text().toDouble()
244 winstr = str(self.gui.filterDesignTypeComboBox.currentText().toAscii())
245 ftype = str(self.gui.filterTypeComboBox.currentText().toAscii())
247 if(winstr == "Equiripple"):
248 designer = {"Low Pass" : self.design_opt_lpf,
249 "Band Pass" : self.design_opt_bpf,
250 "Complex Band Pass" : self.design_opt_cbpf,
251 "Band Notch" : self.design_opt_bnf,
252 "High Pass" : self.design_opt_hpf}
253 taps,r = designer[ftype](fs, gain)
256 designer = {"Low Pass" : self.design_win_lpf,
257 "Band Pass" : self.design_win_bpf,
258 "Complex Band Pass" : self.design_win_cbpf,
259 "Band Notch" : self.design_win_bnf,
260 "High Pass" : self.design_win_hpf,
261 "Root Raised Cosine" : self.design_win_rrc,
262 "Gaussian" : self.design_win_gaus}
263 wintype = self.filterWindows[winstr]
264 taps,r = designer[ftype](fs, gain, wintype)
267 self.taps = scipy.array(taps)
268 self.get_fft(fs, self.taps, self.nfftpts)
269 self.update_time_curves()
270 self.update_freq_curves()
271 self.update_phase_curves()
272 self.update_group_curves()
274 self.gui.nTapsEdit.setText(Qt.QString("%1").arg(self.taps.size))
277 # Filter design functions using a window
278 def design_win_lpf(self, fs, gain, wintype):
280 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
282 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
284 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
290 taps = gr.firdes.low_pass_2(gain, fs, pb, tb,
296 def design_win_bpf(self, fs, gain, wintype):
298 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
300 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
302 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
304 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
308 taps = gr.firdes.band_pass_2(gain, fs, pb1, pb2, tb,
314 def design_win_cbpf(self, fs, gain, wintype):
316 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
318 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
320 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
322 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
326 taps = gr.firdes.complex_band_pass_2(gain, fs, pb1, pb2, tb,
332 def design_win_bnf(self, fs, gain, wintype):
334 pb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
336 pb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
338 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
340 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
344 taps = gr.firdes.band_reject_2(gain, fs, pb1, pb2, tb,
350 def design_win_hpf(self, fs, gain, wintype):
352 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
354 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
356 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
361 taps = gr.firdes.high_pass_2(gain, fs, pb, tb,
367 def design_win_rrc(self, fs, gain, wintype):
369 sr,r = self.gui.rrcSymbolRateEdit.text().toDouble()
371 alpha,r = self.gui.rrcAlphaEdit.text().toDouble()
373 ntaps,r = self.gui.rrcNumTapsEdit.text().toInt()
377 taps = gr.firdes.root_raised_cosine(gain, fs, sr,
383 def design_win_gaus(self, fs, gain, wintype):
385 sr,r = self.gui.gausSymbolRateEdit.text().toDouble()
387 bt,r = self.gui.gausBTEdit.text().toDouble()
389 ntaps,r = self.gui.gausNumTapsEdit.text().toInt()
394 taps = gr.firdes.gaussian(gain, spb, bt, ntaps)
399 # Design Functions for Equiripple Filters
400 def design_opt_lpf(self, fs, gain):
402 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
404 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
406 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
408 ripple,r = self.gui.lpfPassBandRippleEdit.text().toDouble()
412 taps = blks2.optfir.low_pass(gain, fs, pb, sb,
418 def design_opt_bpf(self, fs, gain):
420 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
422 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
424 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
426 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
428 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
434 taps = blks2.optfir.band_pass(gain, fs, sb1, pb1, pb2, sb2,
440 def design_opt_cbpf(self, fs, gain):
442 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
444 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
446 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
448 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
450 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
456 taps = blks2.optfir.complex_band_pass(gain, fs, sb1, pb1, pb2, sb2,
462 def design_opt_bnf(self, fs, gain):
464 sb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
466 sb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
468 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
470 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
472 ripple,r = self.gui.bnfPassBandRippleEdit.text().toDouble()
478 taps = blks2.optfir.band_reject(gain, fs, pb1, sb1, sb2, pb2,
484 def design_opt_hpf(self, fs, gain):
486 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
488 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
490 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
492 ripple,r = self.gui.hpfPassBandRippleEdit.text().toDouble()
496 taps = blks2.optfir.high_pass(gain, fs, sb, pb,
502 def nfft_edit_changed(self, nfft):
503 infft,r = nfft.toInt()
504 if(r and (infft != self.nfftpts)):
506 self.update_freq_curves()
508 def tab_changed(self, tab):
510 self.update_freq_curves()
512 self.update_time_curves()
514 self.update_phase_curves()
516 self.update_group_curves()
518 def get_fft(self, fs, taps, Npts):
520 fftpts = fftpack.fft(taps, Npts)
521 self.freq = scipy.arange(0, fs, 1.0/(Npts*Ts))
522 self.fftdB = 20.0*scipy.log10(abs(fftpts))
523 self.fftDeg = scipy.unwrap(scipy.angle(fftpts))
524 self.groupDelay = -scipy.diff(self.fftDeg)
526 def update_time_curves(self):
527 ntaps = len(self.taps)
529 if(type(self.taps[0]) == scipy.complex128):
530 self.rcurve.setData(scipy.arange(ntaps), self.taps.real)
531 self.icurve.setData(scipy.arange(ntaps), self.taps.imag)
533 self.rcurve.setData(scipy.arange(ntaps), self.taps)
535 # Reset the x-axis to the new time scale
536 ymax = 1.5 * max(self.taps)
537 ymin = 1.5 * min(self.taps)
538 self.gui.timePlot.setAxisScale(self.gui.timePlot.xBottom,
540 self.gui.timePlot.setAxisScale(self.gui.timePlot.yLeft,
543 # Set the zoomer base to unzoom to the new axis
544 self.timeZoomer.setZoomBase()
546 self.gui.timePlot.replot()
548 def update_freq_curves(self):
549 npts = len(self.fftdB)
551 self.freqcurve.setData(self.freq, self.fftdB)
553 # Reset the x-axis to the new time scale
554 ymax = 1.5 * max(self.fftdB[0:npts/2])
555 ymin = 1.1 * min(self.fftdB[0:npts/2])
556 xmax = self.freq[npts/2]
558 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.xBottom,
560 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.yLeft,
563 # Set the zoomer base to unzoom to the new axis
564 self.freqZoomer.setZoomBase()
566 self.gui.freqPlot.replot()
569 def update_phase_curves(self):
570 npts = len(self.fftDeg)
572 self.phasecurve.setData(self.freq, self.fftDeg)
574 # Reset the x-axis to the new time scale
575 ymax = 1.5 * max(self.fftDeg[0:npts/2])
576 ymin = 1.1 * min(self.fftDeg[0:npts/2])
577 xmax = self.freq[npts/2]
579 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.xBottom,
581 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.yLeft,
584 # Set the zoomer base to unzoom to the new axis
585 self.phaseZoomer.setZoomBase()
587 self.gui.phasePlot.replot()
589 def update_group_curves(self):
590 npts = len(self.groupDelay)
592 self.groupcurve.setData(self.freq, self.groupDelay)
594 # Reset the x-axis to the new time scale
595 ymax = 1.5 * max(self.groupDelay[0:npts/2])
596 ymin = 1.1 * min(self.groupDelay[0:npts/2])
597 xmax = self.freq[npts/2]
599 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.xBottom,
601 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.yLeft,
604 # Set the zoomer base to unzoom to the new axis
605 self.groupZoomer.setZoomBase()
607 self.gui.groupPlot.replot()
611 usage="%prog: [options] (input_filename)"
614 parser = OptionParser(conflict_handler="resolve",
615 usage=usage, description=description)
619 parser = setup_options()
620 (options, args) = parser.parse_args ()
622 app = Qt.QApplication(args)
623 gplt = gr_plot_filter(app, options)
626 if __name__ == '__main__':