Program.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import numpy as np
  2. import scipy as sp
  3. import control as ct
  4. import csv
  5. import matplotlib.pyplot as plt
  6. import TransferFunctionConstruction as tfc
  7. import TransferFunctionToStateSpace as tftoss
  8. import BalancedTruncation as bt
  9. import ModalTruncation as mt
  10. # NOTE: Uncomment one block of code to obtain data for that case.
  11. ## Basic example
  12. #A, B, C, D = tftoss.ConvertToStateSpace(np.array([1, -5/2, 1, 1, -1/2]), np.array([1/12, 41/84, 67/42, 47/24, 1]))
  13. #SamplingInterval = 1/8000
  14. ## Order 10 filter
  15. TransferFunction, SamplingInterval = tfc.ConstructTransferFunction("../Data/GDBLinear5BQ.csv")
  16. A, B, C, D = tftoss.ConvertToStateSpace(TransferFunction[0], TransferFunction[1])
  17. ## Order 10 filter B
  18. #TransferFunction, SamplingInterval = tfc.ConstructTransferFunction("../Data/GDBLinear5BQ_B.csv")
  19. #A, B, C, D = tftoss.ConvertToStateSpace(TransferFunction[0], TransferFunction[1])
  20. ## Order 40 filter
  21. #TransferFunction, SamplingInterval = tfc.ConstructTransferFunction("../Data/GDBLinear20BQ.csv")
  22. #A, B, C, D = tftoss.ConvertToStateSpace(TransferFunction[0], TransferFunction[1])
  23. ## Order 40 filter B
  24. #TransferFunction, SamplingInterval = tfc.ConstructTransferFunction("../Data/GDBLinear20BQ_B.csv")
  25. #A, B, C, D = tftoss.ConvertToStateSpace(TransferFunction[0], TransferFunction[1])
  26. ReducedOrder = 9
  27. # BalancedTruncation result
  28. A11, B1, C1 = bt.ApplyBalancedTruncation(SamplingInterval, ReducedOrder, A, B, C, D, None)
  29. # Modal Truncation result
  30. A_reduced, B_reduced, C_reduced, residues_reduced, poles_reduced, D = mt.ApplyModalTruncation(ReducedOrder, A, B, C, D, None)
  31. # Plots the impulse response of a given system for a given number of samples.
  32. # First provide the matrices of the original system and then the matrices of the approximation.
  33. # Plotting the impulse response of systems reduced with balanced truncation is possible,
  34. # but systems reduced with modal truncation gives problems as it is a complex system.
  35. # The impulse response of order 10 filter is most interesting for 50 samples,
  36. # but for the order 40 filters the first 30 samples give a useful view.
  37. ImpulseLength = 50
  38. def PlotImpulseResponse(A, B, C, D, A11, B1, C1):
  39. newB = np.asarray(B).reshape(-1)
  40. newC = np.asarray(C).reshape(-1)
  41. newB1 = np.asarray(B1).reshape(-1)
  42. newC1 = np.asarray(C1).reshape(-1)
  43. yOriginal = [D]
  44. xOriginal = []
  45. xOriginal.append(np.zeros(newB.size))
  46. xOriginal.append(newB)
  47. for i in range(1, ImpulseLength):
  48. xOriginal.append(np.matmul(A, xOriginal[-1]))
  49. yOriginal.append(np.matmul(newC, xOriginal[i]))
  50. yApprox = [D]
  51. xApprox = []
  52. xApprox.append(np.zeros(newB1.size))
  53. xApprox.append(newB1)
  54. for i in range(1, ImpulseLength):
  55. xApprox.append(np.matmul(A11, xApprox[-1]))
  56. yApprox.append(np.matmul(newC1, xApprox[i]))
  57. plt.plot(yOriginal, color = 'grey', linestyle = 'dashed')
  58. plt.plot(yApprox, color = 'blue', linewidth = 2.5)
  59. plt.show()
  60. PlotImpulseResponse(A, B, C, D, A, B, C)
  61. PlotImpulseResponse(A, B, C, D, A11, B1, C1)