commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r8729 - gnuradio/branches/developers/jblum/gr-wxglgui/


From: jblum
Subject: [Commit-gnuradio] r8729 - gnuradio/branches/developers/jblum/gr-wxglgui/src/python
Date: Thu, 26 Jun 2008 14:39:02 -0600 (MDT)

Author: jblum
Date: 2008-06-26 14:39:01 -0600 (Thu, 26 Jun 2008)
New Revision: 8729

Added:
   gnuradio/branches/developers/jblum/gr-wxglgui/src/python/fftsink.py
Modified:
   gnuradio/branches/developers/jblum/gr-wxglgui/src/python/plotter.py
Log:
working fft example

Added: gnuradio/branches/developers/jblum/gr-wxglgui/src/python/fftsink.py
===================================================================
--- gnuradio/branches/developers/jblum/gr-wxglgui/src/python/fftsink.py         
                (rev 0)
+++ gnuradio/branches/developers/jblum/gr-wxglgui/src/python/fftsink.py 
2008-06-26 20:39:01 UTC (rev 8729)
@@ -0,0 +1,20 @@
+#
+# Copyright 2008 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 

Modified: gnuradio/branches/developers/jblum/gr-wxglgui/src/python/plotter.py
===================================================================
--- gnuradio/branches/developers/jblum/gr-wxglgui/src/python/plotter.py 
2008-06-26 19:19:37 UTC (rev 8728)
+++ gnuradio/branches/developers/jblum/gr-wxglgui/src/python/plotter.py 
2008-06-26 20:39:01 UTC (rev 8729)
@@ -1,3 +1,24 @@
+#
+# Copyright 2008 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
+
 import wx
 import wx.glcanvas
 
@@ -10,15 +31,18 @@
 import math
 
 BACKGROUND_COLOR_SPEC = (1, 0.976, 1, 1) #creamy white
-GRID_LINE_COLOR_SPEC = (0, 0, 0, 1) #black
+GRID_LINE_COLOR_SPEC = (0, 0, 0) #black
 
-TICK_TEXT_COLOR_SPEC = (0, 0, 0, 1) #black
+TICK_TEXT_COLOR_SPEC = (0, 0, 0) #black
 TICK_TEXT_FONT_SIZE = 9
 
-UNITS_TEXT_COLOR_SPEC = (0, 0, 0, 1) #black
+TITLE_TEXT_COLOR_SPEC = (0, 0, 0) #black
+TITLE_TEXT_FONT_SIZE = 13
+
+UNITS_TEXT_COLOR_SPEC = (0, 0, 0) #black
 UNITS_TEXT_FONT_SIZE = 9
 
-GRID_COMPILED_LIST_ID = 1
+GRID_COMPILED_LIST_ID = wx.NewId()
 
 class _plotter_base(wx.glcanvas.GLCanvas):
        """!
@@ -61,26 +85,21 @@
                        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)             
                
                        glClearColor(*BACKGROUND_COLOR_SPEC)
                        self.OnSize(None)
-                       self._gl_init = True            
-               self.update()
+                       self._gl_init = True
+               self.draw()
                return
                
-       def update(self): pass
+       def update(self): wx.PostEvent(self, wx.PaintEvent())
        
-       def clear(self): glClear(GL_COLOR_BUFFER_BIT)
-               
-       def draw(self):
-               """!
-               Draw to the screen.
-               Actually swap the buffers and clear the old buffer.
-               """
-               self.SwapBuffers()
-               self.clear()    
+       def clear(self): glClear(GL_COLOR_BUFFER_BIT)           
 
 class grid_plotter(_plotter_base):
        
-       def __init__(self, parent, units_x, units_y, padding_top, 
padding_right, padding_bottom, padding_left):
+       def __init__(self, parent, title, units_x, units_y, padding_top, 
padding_right, padding_bottom, padding_left):
                
+               self.channels = dict()
+               
+               self.title = title
                self.units_x = units_x
                self.units_y = units_y
                
@@ -89,8 +108,8 @@
                self.padding_bottom = padding_bottom
                self.padding_left = padding_left
                
-               self.set_grid_x(-1, 1, .2)              
-               self.set_grid_y(-1, 1, .4)
+               self.set_grid_x(-1, 1, 1)               
+               self.set_grid_y(-1, 1, 1)
                
                _plotter_base.__init__(self, parent)
 
@@ -118,26 +137,52 @@
                self.y_step = float(y_step)
                self._changed = True
        
-       def update(self):
+       def draw(self):
+               """!
+               Draw the grid and waveforms.
+               """
                self.clear()
+               #store the grid drawing operations
                if self._changed:
                        glNewList(GRID_COMPILED_LIST_ID, GL_COMPILE)
                        self._draw_grid()
                        glEndList()
                        self._changed = False
+               ##################################################
+               # Draw Waveforms
+               ##################################################
+               for channel, (samples, offset, color_spec) in 
self.channels.iteritems():
+                       glColor3f(*color_spec)
+                       glBegin(GL_LINE_STRIP)
+                       num_samps = len(samples)
+                       for i, samp in enumerate(samples):      
+                               x = 
(self.width-self.padding_left-self.padding_right)*i/float(num_samps-1) + 
self.padding_left
+                               y = 
(self.height-self.padding_top-self.padding_bottom)*(1 - (samp + offset - 
self.y_min)/(self.y_max-self.y_min)) + self.padding_top
+                               glVertex3f(x, y, 0)
+                       glEnd()
+               #draw the grid
                glCallList(GRID_COMPILED_LIST_ID)
-               self.draw()
-               return
-                                       
+               glFlush()
+               self.SwapBuffers()
+       
        def _draw_grid(self):
                """!
-               Draw the border, grid, and units.
+               Draw the border, grid, title, and units.
                Save the commands to a compiled list.
                """
                ##################################################
+               # Clear Around Border
+               ##################################################
+               glColor4f(*BACKGROUND_COLOR_SPEC)
+               self._draw_rect(0, 0, self.width, self.padding_top)
+               self._draw_rect(self.width-self.padding_right, 0, 
self.padding_right, self.height)
+               self._draw_rect(0, self.height-self.padding_bottom, self.width, 
self.padding_bottom)
+               self._draw_rect(0, 0, self.padding_left, self.height)
+               
+               ##################################################
                # Draw Border
                ##################################################
-               glColor4f(*GRID_LINE_COLOR_SPEC)
+               glColor3f(*GRID_LINE_COLOR_SPEC)
                glBegin(GL_LINE_LOOP)
                glVertex3f(self.padding_left, self.padding_top, 0)
                glVertex3f(self.width - self.padding_right, self.padding_top, 0)
@@ -151,7 +196,7 @@
                for tick in self._get_ticks(self.x_min, self.x_max, 
self.x_step):
                        scaled_tick = 
(self.width-self.padding_left-self.padding_right)*(tick-self.x_min)/(self.x_max-self.x_min)
 + self.padding_left
                        
-                       glColor4f(*GRID_LINE_COLOR_SPEC)
+                       glColor3f(*GRID_LINE_COLOR_SPEC)
                        self._draw_line(
                                (scaled_tick, self.padding_top, 0), 
                                (scaled_tick, self.height-self.padding_bottom, 
0), 
@@ -164,7 +209,7 @@
                for tick in self._get_ticks(self.y_min, self.y_max, 
self.y_step):
                        scaled_tick = 
(self.height-self.padding_top-self.padding_bottom)*(1 - 
(tick-self.y_min)/(self.y_max-self.y_min)) + self.padding_top
                        
-                       glColor4f(*GRID_LINE_COLOR_SPEC)
+                       glColor3f(*GRID_LINE_COLOR_SPEC)
                        self._draw_line(
                                (self.padding_left, scaled_tick, 0), 
                                (self.width-self.padding_right, scaled_tick, 
0), 
@@ -172,9 +217,19 @@
                        self._draw_tick_label(tick, (.75*self.padding_left, 
scaled_tick))
                        
                ##################################################
+               # Draw Title
+               ##################################################      
+               glColor3f(*TITLE_TEXT_COLOR_SPEC)                       
+               font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
+               font.SetWeight(wx.FONTWEIGHT_BOLD)
+               #draw x units
+               txt = gltext.Text(self.title, font=font, 
font_size=TITLE_TEXT_FONT_SIZE, centered=True)
+               txt.draw_text(wx.Point(self.width/2.0, .5*self.padding_top))
+               
+               ##################################################
                # Draw Units
                ##################################################
-               glColor4f(*UNITS_TEXT_COLOR_SPEC)                       
+               glColor3f(*UNITS_TEXT_COLOR_SPEC)                       
                font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
                font.SetWeight(wx.FONTWEIGHT_BOLD)      
                #draw x units
@@ -192,7 +247,7 @@
                @param coor the x, y coordinate
                """             
                tick_str = '%.3g'%float(tick)           
-               glColor4f(*TICK_TEXT_COLOR_SPEC)                                
+               glColor3f(*TICK_TEXT_COLOR_SPEC)                                
                txt = gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE, 
centered=True)
                txt.draw_text(wx.Point(*coor))
        
@@ -227,11 +282,93 @@
                glVertex3f(*coor1)
                glVertex3f(*coor2)
                glEnd()
+               
+       def _draw_rect(self, x, y, width, height):
+               """!
+               Draw a rectangle on the x, y plane.
+               X and Y are the top-left corner.
+               @param x the left position of the rectangle
+               @param y the top position of the rectangle
+               @param width the width of the rectangle
+               @param height the height of the rectangle
+               """
+               glBegin(GL_QUADS)
+               glVertex3f(x, y, 0)
+               glVertex3f(x+width, y, 0)
+               glVertex3f(x+width, y+height, 0)
+               glVertex3f(x, y+height, 0)
+               glEnd()
+               
+       def set_waveform(self, channel, samples, offset, color_spec):
+               """!
+               Set the waveform for a given channel.
+               @param channel the channel number
+               @param samples the waveform samples
+               @param offset the offset to add to each sample
+               @param color_spec the 3-tuple for line color
+               """
+               self.channels[channel] = samples, offset, color_spec
 
+from gnuradio import gr, window
+class test_block(gr.top_block):
+       def __init__(self):
+               gr.top_block.__init__(self)
+
+               fft_size = 256
+
+               input_rate = 55.48e3
+
+               src = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2.32e3, 1)
+               gr_noise_source_x = gr.noise_source_c(gr.GR_GAUSSIAN, 1, 42)
+               
+               gr_add_vxx = gr.add_vcc(1)
+               
+               thr = gr.throttle(gr.sizeof_gr_complex, input_rate)
+                                                       
+               gr_stream_to_vector = 
gr.stream_to_vector(gr.sizeof_gr_complex*1, fft_size)
+               gr_fft_vxx = gr.fft_vcc(fft_size, True, 
(window.blackmanharris(fft_size)))
+               gr_complex_to_mag_fft = gr.complex_to_mag(fft_size)
+               
+               self.msgq = gr.msg_queue(1)
+               sink = gr.message_sink(gr.sizeof_float * fft_size, self.msgq, 
True)             
+               
+               self.connect(src, gr_add_vxx, thr, gr_stream_to_vector, 
gr_fft_vxx, gr_complex_to_mag_fft, sink)
+               self.connect(gr_noise_source_x, (gr_add_vxx, 1))
+
+import threading
+import numpy
+class animate(threading.Thread):
+       def __init__(self, plotter):
+               self.plotter = plotter
+               self.tb = test_block()
+               threading.Thread.__init__(self)
+               self.start()
+
+       def run(self):
+               self.plotter.set_grid_x(-1, 1, .2)              
+               self.plotter.set_grid_y(-30, 75, 30)
+               self.tb.start()
+               while 1:
+                       msg = self.tb.msgq.delete_head()  # blocking read of 
message queue
+                       itemsize = int(msg.arg1())
+                       nitems = int(msg.arg2())
+                       s = msg.to_string()
+                       if nitems > 1:
+                               start = itemsize * (nitems - 1)
+                               s = s[start:start+itemsize]
+
+
+                       arr = numpy.fromstring(s, numpy.float32)
+                       self.plotter.set_waveform(1, arr, -10, (0, 1, 0))
+                       self.plotter.set_waveform(2, arr, -20, (0, 0, 1))
+                       self.plotter.update()
 def main():
        app = wx.PySimpleApp()
-       frame = wx.Frame(None, -1, 'test', wx.DefaultPosition, wx.Size(700, 
300))
-       canvas = grid_plotter(frame, 'Frequency(Hz)', 'Amplitude(dB)', 10, 10, 
40, 60)
+       frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition, wx.Size(700, 
300))
+       plotter = grid_plotter(frame, 'Demo Grid', 'Frequency(Hz)', 
'Amplitude(dB)', 35, 10, 40, 60)
+       plotter.set_grid_x(-1, 1, .2)           
+       plotter.set_grid_y(-1, 1, .4)
+       animate(plotter)
        frame.Show()
        app.MainLoop()
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]