FIR.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os.path
  2. import numpy as np
  3. import itertools
  4. import Tools
  5. from scipy import signal
  6. from pylab import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show,semilogx, semilogy
  7. # Those patterns are used for tests and benchmarks.
  8. # For tests, there is the need to add tests for saturation
  9. def cartesian(*somelists):
  10. r=[]
  11. for element in itertools.product(*somelists):
  12. r.append(element)
  13. return(r)
  14. def writeTests(config,format):
  15. NBSAMPLES=256
  16. NUMTAPS = 64
  17. samples=np.random.randn(NBSAMPLES)
  18. refs=np.random.randn(NBSAMPLES)
  19. taps=np.random.randn(NUMTAPS)
  20. samples = Tools.normalize(samples)
  21. refs = Tools.normalize(refs)
  22. taps = Tools.normalize(taps)
  23. ### For benchmarks
  24. config.writeInput(1, samples,"Samples")
  25. config.writeInput(1, taps,"Coefs")
  26. config.writeInput(1, refs,"Refs")
  27. ### For tests
  28. # blocksize 1 2 3 8 11
  29. # taps 1 2 3 4 5 6 7 8 11 25
  30. # state numTaps + blockSize - 1
  31. # ref blockSize
  32. # Maximum number of samples for all tested FIR configurations is 2*23
  33. t = np.linspace(0, 1, 2*23)
  34. x = np.sin(2*np.pi*50*t)+np.random.randn(len(t)) * 0.08
  35. x = Tools.normalize(x)
  36. # To avoid saturation
  37. x = x / 30.0
  38. config.writeInput(1, x,"FirInput")
  39. tapConfigs=[]
  40. output=[]
  41. defs=[]
  42. if format == 0 or format == 31:
  43. blk = [1, 2, 3, 8, 9,10,11, 16, 23]
  44. taps = [1, 2, 3, 4, 5, 6, 7, 8, 11, 16, 23, 25]
  45. elif format == 15:
  46. blk = [1, 2, 3, 12,13,14,15]
  47. taps = [2, 3, 4, 5, 6, 7, 8, 11, 25]
  48. elif format == 7:
  49. blk = [1, 2, 3 ,20,21,22,23]
  50. taps = [1, 2, 3, 4, 5, 6, 7, 8, 11, 25]
  51. configs = cartesian(blk,taps)
  52. nb=1
  53. for (b,t) in configs:
  54. nbTaps=t
  55. # nbTaps + 2 to be sure all coefficients are not saturated
  56. pythonCoefs = np.array(list(range(1,nbTaps+1)))/(1.0*(nbTaps+2))
  57. coefs=pythonCoefs
  58. if format == 15:
  59. if t % 2 == 1:
  60. nbTaps = nbTaps + 1
  61. coefs = np.append(coefs,[0.0])
  62. out=signal.lfilter(pythonCoefs,[1.0],x[0:2*b])
  63. output += list(out)
  64. coefs = list(coefs)
  65. coefs.reverse()
  66. tapConfigs += coefs
  67. defs += [b,nbTaps]
  68. nb = nb + 1
  69. config.writeInput(1, output,"FirRefs")
  70. config.writeInput(1, tapConfigs,"FirCoefs")
  71. config.writeReferenceS16(1,defs,"FirConfigs")
  72. def generatePatterns():
  73. PATTERNDIR = os.path.join("Patterns","DSP","Filtering","FIR","FIR")
  74. PARAMDIR = os.path.join("Parameters","DSP","Filtering","FIR","FIR")
  75. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  76. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  77. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  78. configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
  79. writeTests(configf32,0)
  80. writeTests(configq31,31)
  81. writeTests(configq15,15)
  82. writeTests(configq7,7)
  83. if __name__ == '__main__':
  84. generatePatterns()