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
18 from pyqt_filter_firlpf import Ui_firlpfForm
19 from pyqt_filter_firhpf import Ui_firhpfForm
21 class gr_plot_filter(QtGui.QMainWindow):
22 def __init__(self, qapp, options):
23 QtGui.QWidget.__init__(self, None)
24 self.gui = Ui_MainWindow()
25 self.gui.setupUi(self)
27 self.connect(self.gui.filterTypeComboBox,
28 Qt.SIGNAL("currentIndexChanged(const QString&)"),
29 self.changed_filter_type)
30 self.connect(self.gui.filterDesignTypeComboBox,
31 Qt.SIGNAL("currentIndexChanged(const QString&)"),
32 self.changed_filter_design_type)
34 self.connect(self.gui.designButton,
35 Qt.SIGNAL("released()"),
38 self.connect(self.gui.tabGroup,
39 Qt.SIGNAL("currentChanged(int)"),
42 self.connect(self.gui.nfftEdit,
43 Qt.SIGNAL("textEdited(QString)"),
44 self.nfft_edit_changed)
46 self.gui.designButton.setShortcut("Return")
52 self.nfftpts = int(10000)
53 self.gui.nfftEdit.setText(Qt.QString("%1").arg(self.nfftpts))
55 self.gui.lpfPassBandRippleLabel.setVisible(False)
56 self.gui.lpfPassBandRippleEdit.setVisible(False)
57 self.gui.bpfPassBandRippleLabel.setVisible(False)
58 self.gui.bpfPassBandRippleEdit.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))
131 self.filterWindows = {"Hamming Window" : gr.firdes.WIN_HAMMING,
132 "Hann Window" : gr.firdes.WIN_HANN,
133 "Blackman Window" : gr.firdes.WIN_BLACKMAN,
134 "Rectangular Window" : gr.firdes.WIN_RECTANGULAR,
135 "Kaiser Window" : gr.firdes.WIN_KAISER,
136 "Blackman-harris Window" : gr.firdes.WIN_BLACKMAN_hARRIS}
140 def changed_filter_type(self, ftype):
141 strftype = str(ftype.toAscii())
142 if(ftype == "Low Pass"):
143 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firlpfPage)
144 elif(ftype == "Band Pass"):
145 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbpfPage)
146 elif(ftype == "Complex Band Pass"):
147 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbpfPage)
148 elif(ftype == "Band Notch"):
149 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firbnfPage)
150 elif(ftype == "High Pass"):
151 self.gui.filterTypeWidget.setCurrentWidget(self.gui.firhpfPage)
152 elif(ftype == "Root Raised Cosine"):
153 self.gui.filterTypeWidget.setCurrentWidget(self.gui.rrcPage)
154 elif(ftype == "Gaussian"):
155 self.gui.filterTypeWidget.setCurrentWidget(self.gui.gausPage)
159 def changed_filter_design_type(self, design):
160 if(design == "Equiripple"):
161 self.set_equiripple()
167 def set_equiripple(self):
168 self.equiripple = True
169 self.gui.lpfPassBandRippleLabel.setVisible(True)
170 self.gui.lpfPassBandRippleEdit.setVisible(True)
171 self.gui.bpfPassBandRippleLabel.setVisible(True)
172 self.gui.bpfPassBandRippleEdit.setVisible(True)
173 self.gui.hpfPassBandRippleLabel.setVisible(True)
174 self.gui.hpfPassBandRippleEdit.setVisible(True)
176 def set_windowed(self):
177 self.equiripple = False
178 self.gui.lpfPassBandRippleLabel.setVisible(False)
179 self.gui.lpfPassBandRippleEdit.setVisible(False)
180 self.gui.bpfPassBandRippleLabel.setVisible(False)
181 self.gui.bpfPassBandRippleEdit.setVisible(False)
182 self.gui.hpfPassBandRippleLabel.setVisible(False)
183 self.gui.hpfPassBandRippleEdit.setVisible(False)
187 fs,r = self.gui.sampleRateEdit.text().toDouble()
189 gain,r = self.gui.filterGainEdit.text().toDouble()
193 winstr = str(self.gui.filterDesignTypeComboBox.currentText().toAscii())
194 ftype = str(self.gui.filterTypeComboBox.currentText().toAscii())
196 if(winstr == "Equiripple"):
197 designer = {"Low Pass" : self.design_opt_lpf,
198 "Band Pass" : self.design_opt_bpf,
199 "High Pass" : self.design_opt_hpf}
200 taps,r = designer[ftype](fs, gain)
203 designer = {"Low Pass" : self.design_win_lpf,
204 "Band Pass" : self.design_win_bpf,
205 "Complex Band Pass" : self.design_win_cbpf,
206 "Band Notch" : self.design_win_bnf,
207 "High Pass" : self.design_win_hpf,
208 "Root Raised Cosine" : self.design_win_rrc,
209 "Gaussian" : self.design_win_gaus}
210 wintype = self.filterWindows[winstr]
211 taps,r = designer[ftype](fs, gain, wintype)
214 self.taps = scipy.array(taps)
215 self.get_fft(fs, self.taps, self.nfftpts)
216 self.update_time_curves()
217 self.update_freq_curves()
218 self.update_phase_curves()
219 self.update_group_curves()
221 # Filter design functions using a window
222 def design_win_lpf(self, fs, gain, wintype):
224 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
226 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
228 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
234 taps = gr.firdes.low_pass_2(gain, fs, pb, tb,
240 def design_win_bpf(self, fs, gain, wintype):
242 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
244 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
246 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
248 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
252 taps = gr.firdes.band_pass_2(gain, fs, pb1, pb2, tb,
258 def design_win_cbpf(self, fs, gain, wintype):
260 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
262 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
264 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
266 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
270 taps = gr.firdes.complex_band_pass_2(gain, fs, pb1, pb2, tb,
276 def design_win_bnf(self, fs, gain, wintype):
278 pb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
280 pb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
282 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
284 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
288 taps = gr.firdes.band_reject_2(gain, fs, pb1, pb2, tb,
294 def design_win_hpf(self, fs, gain, wintype):
296 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
298 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
300 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
305 taps = gr.firdes.high_pass_2(gain, fs, pb, tb,
311 def design_win_rrc(self, fs, gain, wintype):
313 sr,r = self.gui.rrcSymbolRateEdit.text().toDouble()
315 alpha,r = self.gui.rrcAlphaEdit.text().toDouble()
317 ntaps,r = self.gui.rrcNumTapsEdit.text().toInt()
321 taps = gr.firdes.root_raised_cosine(gain, fs, sr,
327 def design_win_gaus(self, fs, gain, wintype):
329 sr,r = self.gui.gausSymbolRateEdit.text().toDouble()
331 bt,r = self.gui.gausBTEdit.text().toDouble()
333 ntaps,r = self.gui.gausNumTapsEdit.text().toInt()
338 taps = gr.firdes.gaussian(gain, spb, bt, ntaps)
343 # Design Functions for Equiripple Filters
344 def design_opt_lpf(self, fs, gain, wintype=None):
346 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
348 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
350 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
352 ripple,r = self.gui.lpfPassBandRippleEdit.text().toDouble()
356 taps = blks2.optfir.low_pass(gain, fs, pb, sb,
362 def design_opt_bpf(self, fs, gain, wintype=None):
364 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
366 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
368 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
370 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
372 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
378 taps = blks2.optfir.band_pass(gain, fs, sb1, pb1, pb2, sb2,
384 def design_opt_hpf(self, fs, gain, wintype=None):
386 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
388 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
390 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
392 ripple,r = self.gui.hpfPassBandRippleEdit.text().toDouble()
396 taps = blks2.optfir.high_pass(gain, fs, sb, pb,
402 def nfft_edit_changed(self, nfft):
403 infft,r = nfft.toInt()
404 if(r and (infft != self.nfftpts)):
406 self.update_freq_curves()
408 def tab_changed(self, tab):
410 self.update_freq_curves()
412 self.update_time_curves()
414 self.update_phase_curves()
416 self.update_group_curves()
418 def get_fft(self, fs, taps, Npts):
420 fftpts = fftpack.fft(taps, Npts)
421 self.freq = scipy.arange(0, fs, 1.0/(Npts*Ts))
422 self.fftdB = 20.0*scipy.log10(abs(fftpts))
423 self.fftDeg = scipy.unwrap(scipy.angle(fftpts))
424 self.groupDelay = -scipy.diff(self.fftDeg)
426 def update_time_curves(self):
427 ntaps = len(self.taps)
429 if(type(self.taps[0]) == scipy.complex128):
430 self.rcurve.setData(scipy.arange(ntaps), self.taps.real)
431 self.icurve.setData(scipy.arange(ntaps), self.taps.imag)
433 self.rcurve.setData(scipy.arange(ntaps), self.taps)
435 # Reset the x-axis to the new time scale
436 ymax = 1.5 * max(self.taps)
437 ymin = 1.5 * min(self.taps)
438 self.gui.timePlot.setAxisScale(self.gui.timePlot.xBottom,
440 self.gui.timePlot.setAxisScale(self.gui.timePlot.yLeft,
443 # Set the zoomer base to unzoom to the new axis
444 self.timeZoomer.setZoomBase()
446 self.gui.timePlot.replot()
448 def update_freq_curves(self):
449 npts = len(self.fftdB)
451 self.freqcurve.setData(self.freq, self.fftdB)
453 # Reset the x-axis to the new time scale
454 ymax = 1.5 * max(self.fftdB[0:npts/2])
455 ymin = 1.1 * min(self.fftdB[0:npts/2])
456 xmax = self.freq[npts/2]
458 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.xBottom,
460 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.yLeft,
463 # Set the zoomer base to unzoom to the new axis
464 self.freqZoomer.setZoomBase()
466 self.gui.freqPlot.replot()
469 def update_phase_curves(self):
470 npts = len(self.fftDeg)
472 self.phasecurve.setData(self.freq, self.fftDeg)
474 # Reset the x-axis to the new time scale
475 ymax = 1.5 * max(self.fftDeg[0:npts/2])
476 ymin = 1.1 * min(self.fftDeg[0:npts/2])
477 xmax = self.freq[npts/2]
479 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.xBottom,
481 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.yLeft,
484 # Set the zoomer base to unzoom to the new axis
485 self.phaseZoomer.setZoomBase()
487 self.gui.phasePlot.replot()
489 def update_group_curves(self):
490 npts = len(self.groupDelay)
492 self.groupcurve.setData(self.freq, self.groupDelay)
494 # Reset the x-axis to the new time scale
495 ymax = 1.5 * max(self.groupDelay[0:npts/2])
496 ymin = 1.1 * min(self.groupDelay[0:npts/2])
497 xmax = self.freq[npts/2]
499 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.xBottom,
501 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.yLeft,
504 # Set the zoomer base to unzoom to the new axis
505 self.groupZoomer.setZoomBase()
507 self.gui.groupPlot.replot()
511 usage="%prog: [options] (input_filename)"
514 parser = OptionParser(conflict_handler="resolve",
515 usage=usage, description=description)
519 parser = setup_options()
520 (options, args) = parser.parse_args ()
522 app = Qt.QApplication(args)
523 gplt = gr_plot_filter(app, options)
526 if __name__ == '__main__':