graph.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from cmsisdsp.sdf.scheduler import *
  2. AUDIO_INTERRUPT_LENGTH = 160
  3. MFCCFEATURESSIZE=10
  4. NBOUTMFCC=50
  5. OVERLAPMFCC=25
  6. floatType=CType(F32)
  7. ### Define new types of Nodes
  8. class Node(GenericNode):
  9. def __init__(self,name,theType,inLength,outLength):
  10. GenericNode.__init__(self,name)
  11. self.addInput("i",theType,inLength)
  12. self.addOutput("o",theType,outLength)
  13. @property
  14. def typeName(self):
  15. return "Node"
  16. class StereoSource(GenericSource):
  17. def __init__(self,name,inLength):
  18. GenericSource.__init__(self,name)
  19. self.addOutput("o",floatType,2*inLength)
  20. @property
  21. def typeName(self):
  22. return "StereoSource"
  23. # This is a fake TFLite node just for illustration
  24. class TFLite(GenericSink):
  25. def __init__(self,name):
  26. GenericSink.__init__(self,name)
  27. self.addInput("i",floatType,NBOUTMFCC*MFCCFEATURESSIZE)
  28. @property
  29. def typeName(self):
  30. return "TFLite"
  31. # This is a fake MFCC just to illustrate how it could be used in a graph.
  32. # For a real MFCC example, look at example5
  33. class MFCC(GenericNode):
  34. def __init__(self,name,inLength):
  35. GenericNode.__init__(self,name)
  36. self.addInput("i",floatType,inLength)
  37. self.addOutput("o",floatType,MFCCFEATURESSIZE)
  38. @property
  39. def typeName(self):
  40. return "MFCC"
  41. ### Define nodes
  42. half=Constant("HALF")
  43. src=StereoSource("src",AUDIO_INTERRUPT_LENGTH)
  44. toMono=Unzip("toMono",floatType, AUDIO_INTERRUPT_LENGTH)
  45. sa=Dsp("scale",floatType,AUDIO_INTERRUPT_LENGTH)
  46. sb=Dsp("scale",floatType,AUDIO_INTERRUPT_LENGTH)
  47. add=Dsp("add",floatType,AUDIO_INTERRUPT_LENGTH)
  48. audioWindow=SlidingBuffer("audioWin",floatType,640,320)
  49. mfcc=MFCC("mfcc",640)
  50. mfccWindow=SlidingBuffer("mfccWind",floatType,NBOUTMFCC*MFCCFEATURESSIZE,OVERLAPMFCC*MFCCFEATURESSIZE)
  51. tf=TFLite("TFLite")
  52. g = Graph()
  53. g.connectWithDelay(src.o, toMono.i,10)
  54. g.connect(toMono.o1,sa.ia)
  55. g.connect(toMono.o2,sb.ia)
  56. # A constant node as no output
  57. # So it is connected directly
  58. g.connect(half,sa.ib)
  59. g.connect(half,sb.ib)
  60. g.connect(sa.o,add.ia)
  61. g.connect(sb.o,add.ib)
  62. g.connect(add.o,audioWindow.i)
  63. g.connect(audioWindow.o, mfcc.i)
  64. g.connect(mfcc.o,mfccWindow.i)
  65. g.connect(mfccWindow.o,tf.i)
  66. print("Generate graphviz and code")
  67. conf=Configuration()
  68. conf.debugLimit=1
  69. conf.cOptionalArgs="int opt1,int opt2"
  70. #conf.memoryOptimization=True
  71. #print(g.nullVector())
  72. sched = g.computeSchedule(conf)
  73. #print(sched.schedule)
  74. print("Schedule length = %d" % sched.scheduleLength)
  75. print("Memory usage %d bytes" % sched.memory)
  76. #
  77. #conf.codeArray=True
  78. sched.ccode("generated",conf)
  79. with open("test.dot","w") as f:
  80. sched.graphviz(f)