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))
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.bnfPassBandRippleLabel.setVisible(True)
174 self.gui.bnfPassBandRippleEdit.setVisible(True)
175 self.gui.hpfPassBandRippleLabel.setVisible(True)
176 self.gui.hpfPassBandRippleEdit.setVisible(True)
178 def set_windowed(self):
179 self.equiripple = False
180 self.gui.lpfPassBandRippleLabel.setVisible(False)
181 self.gui.lpfPassBandRippleEdit.setVisible(False)
182 self.gui.bpfPassBandRippleLabel.setVisible(False)
183 self.gui.bpfPassBandRippleEdit.setVisible(False)
184 self.gui.bnfPassBandRippleLabel.setVisible(False)
185 self.gui.bnfPassBandRippleEdit.setVisible(False)
186 self.gui.hpfPassBandRippleLabel.setVisible(False)
187 self.gui.hpfPassBandRippleEdit.setVisible(False)
191 fs,r = self.gui.sampleRateEdit.text().toDouble()
193 gain,r = self.gui.filterGainEdit.text().toDouble()
197 winstr = str(self.gui.filterDesignTypeComboBox.currentText().toAscii())
198 ftype = str(self.gui.filterTypeComboBox.currentText().toAscii())
200 if(winstr == "Equiripple"):
201 designer = {"Low Pass" : self.design_opt_lpf,
202 "Band Pass" : self.design_opt_bpf,
203 "Complex Band Pass" : self.design_opt_cbpf,
204 "Band Notch" : self.design_opt_bnf,
205 "High Pass" : self.design_opt_hpf}
206 taps,r = designer[ftype](fs, gain)
209 designer = {"Low Pass" : self.design_win_lpf,
210 "Band Pass" : self.design_win_bpf,
211 "Complex Band Pass" : self.design_win_cbpf,
212 "Band Notch" : self.design_win_bnf,
213 "High Pass" : self.design_win_hpf,
214 "Root Raised Cosine" : self.design_win_rrc,
215 "Gaussian" : self.design_win_gaus}
216 wintype = self.filterWindows[winstr]
217 taps,r = designer[ftype](fs, gain, wintype)
220 print "Number of taps: ", len(taps)
222 self.taps = scipy.array(taps)
223 self.get_fft(fs, self.taps, self.nfftpts)
224 self.update_time_curves()
225 self.update_freq_curves()
226 self.update_phase_curves()
227 self.update_group_curves()
229 # Filter design functions using a window
230 def design_win_lpf(self, fs, gain, wintype):
232 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
234 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
236 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
242 taps = gr.firdes.low_pass_2(gain, fs, pb, tb,
248 def design_win_bpf(self, fs, gain, wintype):
250 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
252 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
254 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
256 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
260 taps = gr.firdes.band_pass_2(gain, fs, pb1, pb2, tb,
266 def design_win_cbpf(self, fs, gain, wintype):
268 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
270 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
272 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
274 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
278 taps = gr.firdes.complex_band_pass_2(gain, fs, pb1, pb2, tb,
284 def design_win_bnf(self, fs, gain, wintype):
286 pb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
288 pb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
290 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
292 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
296 taps = gr.firdes.band_reject_2(gain, fs, pb1, pb2, tb,
302 def design_win_hpf(self, fs, gain, wintype):
304 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
306 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
308 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
313 taps = gr.firdes.high_pass_2(gain, fs, pb, tb,
319 def design_win_rrc(self, fs, gain, wintype):
321 sr,r = self.gui.rrcSymbolRateEdit.text().toDouble()
323 alpha,r = self.gui.rrcAlphaEdit.text().toDouble()
325 ntaps,r = self.gui.rrcNumTapsEdit.text().toInt()
329 taps = gr.firdes.root_raised_cosine(gain, fs, sr,
335 def design_win_gaus(self, fs, gain, wintype):
337 sr,r = self.gui.gausSymbolRateEdit.text().toDouble()
339 bt,r = self.gui.gausBTEdit.text().toDouble()
341 ntaps,r = self.gui.gausNumTapsEdit.text().toInt()
346 taps = gr.firdes.gaussian(gain, spb, bt, ntaps)
351 # Design Functions for Equiripple Filters
352 def design_opt_lpf(self, fs, gain):
354 pb,r = self.gui.endofLpfPassBandEdit.text().toDouble()
356 sb,r = self.gui.startofLpfStopBandEdit.text().toDouble()
358 atten,r = self.gui.lpfStopBandAttenEdit.text().toDouble()
360 ripple,r = self.gui.lpfPassBandRippleEdit.text().toDouble()
364 taps = blks2.optfir.low_pass(gain, fs, pb, sb,
370 def design_opt_bpf(self, fs, gain):
372 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
374 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
376 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
378 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
380 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
386 taps = blks2.optfir.band_pass(gain, fs, sb1, pb1, pb2, sb2,
392 def design_opt_cbpf(self, fs, gain):
394 pb1,r = self.gui.startofBpfPassBandEdit.text().toDouble()
396 pb2,r = self.gui.endofBpfPassBandEdit.text().toDouble()
398 tb,r = self.gui.bpfTransitionEdit.text().toDouble()
400 atten,r = self.gui.bpfStopBandAttenEdit.text().toDouble()
402 ripple,r = self.gui.bpfPassBandRippleEdit.text().toDouble()
408 taps = blks2.optfir.complex_band_pass(gain, fs, sb1, pb1, pb2, sb2,
414 def design_opt_bnf(self, fs, gain):
416 sb1,r = self.gui.startofBnfStopBandEdit.text().toDouble()
418 sb2,r = self.gui.endofBnfStopBandEdit.text().toDouble()
420 tb,r = self.gui.bnfTransitionEdit.text().toDouble()
422 atten,r = self.gui.bnfStopBandAttenEdit.text().toDouble()
424 ripple,r = self.gui.bnfPassBandRippleEdit.text().toDouble()
430 taps = blks2.optfir.band_reject(gain, fs, pb1, sb1, sb2, pb2,
436 def design_opt_hpf(self, fs, gain):
438 sb,r = self.gui.endofHpfStopBandEdit.text().toDouble()
440 pb,r = self.gui.startofHpfPassBandEdit.text().toDouble()
442 atten,r = self.gui.hpfStopBandAttenEdit.text().toDouble()
444 ripple,r = self.gui.hpfPassBandRippleEdit.text().toDouble()
448 taps = blks2.optfir.high_pass(gain, fs, sb, pb,
454 def nfft_edit_changed(self, nfft):
455 infft,r = nfft.toInt()
456 if(r and (infft != self.nfftpts)):
458 self.update_freq_curves()
460 def tab_changed(self, tab):
462 self.update_freq_curves()
464 self.update_time_curves()
466 self.update_phase_curves()
468 self.update_group_curves()
470 def get_fft(self, fs, taps, Npts):
472 fftpts = fftpack.fft(taps, Npts)
473 self.freq = scipy.arange(0, fs, 1.0/(Npts*Ts))
474 self.fftdB = 20.0*scipy.log10(abs(fftpts))
475 self.fftDeg = scipy.unwrap(scipy.angle(fftpts))
476 self.groupDelay = -scipy.diff(self.fftDeg)
478 def update_time_curves(self):
479 ntaps = len(self.taps)
481 if(type(self.taps[0]) == scipy.complex128):
482 self.rcurve.setData(scipy.arange(ntaps), self.taps.real)
483 self.icurve.setData(scipy.arange(ntaps), self.taps.imag)
485 self.rcurve.setData(scipy.arange(ntaps), self.taps)
487 # Reset the x-axis to the new time scale
488 ymax = 1.5 * max(self.taps)
489 ymin = 1.5 * min(self.taps)
490 self.gui.timePlot.setAxisScale(self.gui.timePlot.xBottom,
492 self.gui.timePlot.setAxisScale(self.gui.timePlot.yLeft,
495 # Set the zoomer base to unzoom to the new axis
496 self.timeZoomer.setZoomBase()
498 self.gui.timePlot.replot()
500 def update_freq_curves(self):
501 npts = len(self.fftdB)
503 self.freqcurve.setData(self.freq, self.fftdB)
505 # Reset the x-axis to the new time scale
506 ymax = 1.5 * max(self.fftdB[0:npts/2])
507 ymin = 1.1 * min(self.fftdB[0:npts/2])
508 xmax = self.freq[npts/2]
510 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.xBottom,
512 self.gui.freqPlot.setAxisScale(self.gui.freqPlot.yLeft,
515 # Set the zoomer base to unzoom to the new axis
516 self.freqZoomer.setZoomBase()
518 self.gui.freqPlot.replot()
521 def update_phase_curves(self):
522 npts = len(self.fftDeg)
524 self.phasecurve.setData(self.freq, self.fftDeg)
526 # Reset the x-axis to the new time scale
527 ymax = 1.5 * max(self.fftDeg[0:npts/2])
528 ymin = 1.1 * min(self.fftDeg[0:npts/2])
529 xmax = self.freq[npts/2]
531 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.xBottom,
533 self.gui.phasePlot.setAxisScale(self.gui.phasePlot.yLeft,
536 # Set the zoomer base to unzoom to the new axis
537 self.phaseZoomer.setZoomBase()
539 self.gui.phasePlot.replot()
541 def update_group_curves(self):
542 npts = len(self.groupDelay)
544 self.groupcurve.setData(self.freq, self.groupDelay)
546 # Reset the x-axis to the new time scale
547 ymax = 1.5 * max(self.groupDelay[0:npts/2])
548 ymin = 1.1 * min(self.groupDelay[0:npts/2])
549 xmax = self.freq[npts/2]
551 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.xBottom,
553 self.gui.groupPlot.setAxisScale(self.gui.groupPlot.yLeft,
556 # Set the zoomer base to unzoom to the new axis
557 self.groupZoomer.setZoomBase()
559 self.gui.groupPlot.replot()
563 usage="%prog: [options] (input_filename)"
566 parser = OptionParser(conflict_handler="resolve",
567 usage=usage, description=description)
571 parser = setup_options()
572 (options, args) = parser.parse_args ()
574 app = Qt.QApplication(args)
575 gplt = gr_plot_filter(app, options)
578 if __name__ == '__main__':