summaryrefslogtreecommitdiff
path: root/gr-controls/python
diff options
context:
space:
mode:
Diffstat (limited to 'gr-controls/python')
-rw-r--r--gr-controls/python/CMakeLists.txt94
-rw-r--r--gr-controls/python/__init__.py55
-rw-r--r--gr-controls/python/controls_csim.py1
-rw-r--r--gr-controls/python/controls_dsim.py1
-rw-r--r--gr-controls/python/csim.py44
-rwxr-xr-xgr-controls/python/csim_sci.py38
-rw-r--r--gr-controls/python/dsim.py43
-rw-r--r--gr-controls/python/dsim_sci.py36
-rwxr-xr-xgr-controls/python/qa_dsim.py69
9 files changed, 381 insertions, 0 deletions
diff --git a/gr-controls/python/CMakeLists.txt b/gr-controls/python/CMakeLists.txt
new file mode 100644
index 000000000..b83cb675d
--- /dev/null
+++ b/gr-controls/python/CMakeLists.txt
@@ -0,0 +1,94 @@
+# Copyright 2012 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.
+
+########################################################################
+include(GrPython)
+
+GR_PYTHON_INSTALL(
+ FILES
+ __init__.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+### CSIM BLOCKS ###
+GR_PYTHON_INSTALL(
+ FILES
+ controls_csim.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+
+GR_PYTHON_INSTALL(
+ FILES
+ csim.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+GR_PYTHON_INSTALL(
+ FILES
+ csim_sci.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+### DSIM BLOCKS ###
+GR_PYTHON_INSTALL(
+ FILES
+ controls_dsim.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+GR_PYTHON_INSTALL(
+ FILES
+ dsim.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+GR_PYTHON_INSTALL(
+ FILES
+ dsim_sci.py
+ DESTINATION ${GR_PYTHON_DIR}/gnuradio/controls
+ COMPONENT "controls_python"
+)
+
+
+
+
+########################################################################
+# Handle the unit tests
+########################################################################
+if(ENABLE_TESTING)
+
+list(APPEND GR_TEST_PYTHON_DIRS
+ ${CMAKE_BINARY_DIR}/gr-controls/python
+)
+list(APPEND GR_TEST_TARGET_DEPS gnuradio-controls)
+
+include(GrTest)
+file(GLOB py_qa_test_files "qa_*.py")
+foreach(py_qa_test_file ${py_qa_test_files})
+ get_filename_component(py_qa_test_name ${py_qa_test_file} NAME_WE)
+ GR_ADD_TEST(${py_qa_test_name} ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B} ${py_qa_test_file})
+endforeach(py_qa_test_file)
+endif(ENABLE_TESTING)
diff --git a/gr-controls/python/__init__.py b/gr-controls/python/__init__.py
new file mode 100644
index 000000000..ed63150cf
--- /dev/null
+++ b/gr-controls/python/__init__.py
@@ -0,0 +1,55 @@
+#
+# Copyright 2008,2009 Free Software Foundation, Inc.
+#
+# This application 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.
+#
+# This application 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 this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+
+# The presence of this file turns this directory into a Python package
+
+'''
+This is the GNU Radio SCIMOD module. Place your Python package
+description here (python/__init__.py).
+'''
+
+# ----------------------------------------------------------------
+# Temporary workaround for ticket:181 (swig+python problem)
+import sys
+_RTLD_GLOBAL = 0
+try:
+ from dl import RTLD_GLOBAL as _RTLD_GLOBAL
+except ImportError:
+ try:
+ from DLFCN import RTLD_GLOBAL as _RTLD_GLOBAL
+ except ImportError:
+ pass
+
+if _RTLD_GLOBAL != 0:
+ _dlopenflags = sys.getdlopenflags()
+ sys.setdlopenflags(_dlopenflags|_RTLD_GLOBAL)
+# ----------------------------------------------------------------
+
+
+# import swig generated symbols into the scimod namespace
+from scimod_swig import *
+
+# import any pure python here
+from dsim import dsim
+#
+
+# ----------------------------------------------------------------
+# Tail of workaround
+if _RTLD_GLOBAL != 0:
+ sys.setdlopenflags(_dlopenflags) # Restore original flags
+# ----------------------------------------------------------------
diff --git a/gr-controls/python/controls_csim.py b/gr-controls/python/controls_csim.py
new file mode 100644
index 000000000..45344b60d
--- /dev/null
+++ b/gr-controls/python/controls_csim.py
@@ -0,0 +1 @@
+from csim import *
diff --git a/gr-controls/python/controls_dsim.py b/gr-controls/python/controls_dsim.py
new file mode 100644
index 000000000..aca638a40
--- /dev/null
+++ b/gr-controls/python/controls_dsim.py
@@ -0,0 +1 @@
+from dsim import *
diff --git a/gr-controls/python/csim.py b/gr-controls/python/csim.py
new file mode 100644
index 000000000..3d91eaf15
--- /dev/null
+++ b/gr-controls/python/csim.py
@@ -0,0 +1,44 @@
+#!/usr/bin/python
+import gras
+import numpy
+
+class csim(gras.Block):
+
+ def __init__(self):
+ gras.Block.__init__(self,
+ name="csim",
+ in_sig=[numpy.float32],
+ out_sig=[numpy.float32])
+
+ def set_parameters(self,p,i,d,a,b,d1,e,f):
+ self.param1 = p
+ self.param2 = i
+ self.param3 = d
+ self.param4 = a #n0
+ self.param5 = b #n1
+ self.param6 = d1 #d0
+ self.param7 = e #d1
+ self.n = f #window
+
+ def isIntegralWin(self, input_item, window):
+ if (len(input_item) % window ):
+ raise Exception("Value of Window should be an integral value of length of input items")
+
+ def work(self, input_items, output_items):
+
+ #n = min(len(input_items[0]), len(output_items[0]))
+ in0 = input_items[0]
+ out = output_items[0]
+
+ from csim_sci import csim
+ #Processing
+ # Assuming n = 1 input_config(0)=1
+
+ out[:self.n] = csim(self.param1, self.param2, self.param3, self.param4,
+ self.param5, self.param6, self.param7, in0[:self.n].tolist()) # IMP: in0[:self.n].tolist() passes a python array, without which window cannot be raised above certain value | numpy.array bug
+
+ print "OUT", out[:self.n]
+
+ self.consume(0,self.n) # Consume from port 0 input_items
+ self.produce(0,self.n) # Produce from port 0 output_items
+
diff --git a/gr-controls/python/csim_sci.py b/gr-controls/python/csim_sci.py
new file mode 100755
index 000000000..3d900bc40
--- /dev/null
+++ b/gr-controls/python/csim_sci.py
@@ -0,0 +1,38 @@
+#!/usr/bin/python
+
+import sciscipy
+
+
+# u is a TUPLE vector of width w
+
+def csim(P,I,D,n0,n1,d0,d1,u):
+ code_string1 = "s=%s;"
+ code_string2 = "Gc=syslin('c',("+str(P*I+D)+"*s)"+","+str(I)+"*s);"
+ code_string3 = "G=syslin("
+ code_string4 = "'c'"+","+str(n0)+"*s"+"+"+str(n1)+","+str(d0)+"*s"+"+"+str(d1)+");"
+ code_string5 = "r=tf2ss(G*Gc);"
+ code_string6 = "u="+str((u))+";"
+ code_string7 = "y=csim(u,1:length(u),r)"
+ code_string = code_string1+code_string2+code_string3+code_string4+code_string5+code_string6+code_string7
+ print(code_string)
+
+ import sciscipy
+ sciscipy.eval(code_string)
+ y = sciscipy.read("y")
+ return y
+ #print "y"
+ import matplotlib.pyplot as plt
+ plt.plot(y)
+ plt.show()
+
+
+if __name__ == "__main__":
+ u = [0]*100
+ u[50] = 1
+ out = csim(2,0.5,0.6,1,1,2,1,u)
+ print out
+
+ import matplotlib.pyplot as plt
+ plt.plot(out)
+ plt.show()
+
diff --git a/gr-controls/python/dsim.py b/gr-controls/python/dsim.py
new file mode 100644
index 000000000..3e5e4c8ec
--- /dev/null
+++ b/gr-controls/python/dsim.py
@@ -0,0 +1,43 @@
+import gras
+import numpy
+
+class dsim(gras.Block):
+
+ def __init__(self):
+ gras.Block.__init__(self,
+ name="dsim",
+ in_sig=[numpy.float32],
+ out_sig=[numpy.float32])
+
+ def set_parameters(self,p,i,d,a,b,c,d1,e,f):
+ self.param1 = p
+ self.param2 = i
+ self.param3 = d
+ self.param4 = a #n0
+ self.param5 = b #n1
+ self.param6 = c #st
+ self.param7 = d1 #d0
+ self.param8 = e #d1
+ self.n = f #window
+
+ def isIntegralWin(self, input_item, window):
+ if (len(input_item) % window ):
+ raise Exception("Value of Window should be an integral value of length of input items")
+
+ def work(self, input_items, output_items):
+
+ #n = min(len(input_items[0]), len(output_items[0]))
+ in0 = input_items[0]
+ out = output_items[0]
+
+ from dsim_sci import discrete_sim
+ #Processing
+ # Assuming n = 1 input_config(0)=1
+
+ out[:self.n] = discrete_sim(self.param1, self.param2, self.param3, self.param4,
+ self.param5, self.param6, self.param7, self.param8, in0[:self.n].tolist()) # IMP: in0[:self.n].tolist() passes a python array, without which window cannot be raised above certain value | numpy.array bug
+
+ print out[:self.n], in0[:self.n]
+
+ self.consume(0,self.n) # Consume from port 0 input_items
+ self.produce(0,self.n) # Produce from port 0 output_items
diff --git a/gr-controls/python/dsim_sci.py b/gr-controls/python/dsim_sci.py
new file mode 100644
index 000000000..115c802c4
--- /dev/null
+++ b/gr-controls/python/dsim_sci.py
@@ -0,0 +1,36 @@
+#!/usr/bin/python
+
+import sciscipy
+
+
+# u is a TUPLE vector of width w
+
+def discrete_sim(P,I,D,n0,n1,st,d0,d1,u):
+ code_string1 = "s=%s;"
+ code_string2 = "Gc=syslin("+str(st)+",("+str(P*I+D)+"*s)"+","+str(I)+"*s);"
+ code_string3 = "G=syslin("
+ code_string4 = str(st)+","+str(n0)+"*s"+"+"+str(n1)+","+str(d0)+"*s"+"+"+str(d1)+");"
+ code_string5 = "r=tf2ss(G*Gc);"
+ code_string6 = "u="+str((u))+";"
+ code_string7 = "y=dsimul(r,u)"
+ code_string = code_string1+code_string2+code_string3+code_string4+code_string5+code_string6+code_string7
+
+ # Check complete_code_string
+ #print code_string
+
+ import sciscipy
+ sciscipy.eval(code_string)
+ y = sciscipy.read("y")
+ return y
+
+#print discrete_sim(1,1,0.1,2,1,"u=zeros(1,50);u(10)=1")
+
+if __name__ == "__main__":
+ u = [0]*100
+ u[50] = 1
+ out = discrete_sim(2,0.5,0.6,1,1,0.1,2,1,u)
+ print out
+
+ #import matplotlib.pyplot as plt
+ #plt.plot(out)
+ #plt.show()
diff --git a/gr-controls/python/qa_dsim.py b/gr-controls/python/qa_dsim.py
new file mode 100755
index 000000000..8c0a6be4e
--- /dev/null
+++ b/gr-controls/python/qa_dsim.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+#
+# Copyright 2013 <+YOU OR YOUR COMPANY+>.
+#
+# This 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.
+#
+# This software 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 this software; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr, gr_unittest
+#import mymod_swig as mymod
+from dsim import dsim
+
+
+class qa_dsim (gr_unittest.TestCase):
+
+ def setUp (self):
+ self.tb = gr.top_block ()
+
+ def tearDown (self):
+ self.tb = None
+
+ def test_001_t (self):
+
+ src_data = [0]*100
+ src_data1 = [1]*1000
+ src_data = tuple(src_data+src_data1)
+
+ expected_result = (-2.0, 0.0, 5.0, 8.0, 9.0, 11.0, 14.0, 18.0)
+
+ src0 = gr.vector_source_f(src_data)
+ sqr = dsim()
+ sqr.set_parameters(2,0.5,0.6,1,1, 0.1, 2, 1, 1100)
+
+ #Preload
+ sqr.input_config(1).preload_items = 1
+ dst = gr.vector_sink_f()
+
+ self.tb.connect(src0, (sqr,0)) # src0(vector_source) -> sqr_input_0
+ #self.tb.connect((sqr,0), (sqr,1)) # sqr_output_0 -> sqr_input_1
+ self.tb.connect(sqr,dst) # sqr_output_0 -> dst (vector_source)
+
+ self.tb.run()
+
+ result_data = dst.data()
+ #print str(result_data), "Result data"
+ #print str(expected_result), "expected "
+
+
+ import matplotlib.pyplot as plt
+ plt.plot(result_data)
+ plt.show()
+ #self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
+
+
+if __name__ == '__main__':
+ gr_unittest.main()
+ #gr_unittest.run(qa_dsim, "qa_dsim.xml")