Advertisement
hbinderup94

mirror_python

Feb 28th, 2019
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. class SpejlKilder:
  7.     def __init__(self):
  8.         self.frequency = 1000
  9.         self.c = 0.34
  10.         self.i = (-1) ** (1 / 2)
  11.         self.planes = []
  12.         self.reflections = [[], []]
  13.  
  14.         # Mirror image order
  15.         self.order = 3
  16.  
  17.         # Room dimenstions
  18.         self.Lx = 4
  19.         self.Ly = 5
  20.         self.Lz = 3
  21.  
  22.         # Speaker placement
  23.         self.speaker_x = 2
  24.         self.speaker_y = 3
  25.         self.speaker_z = 1
  26.  
  27.         # Listener Placement
  28.         self.mic_x = 1
  29.         self.mic_y = 3
  30.         self.mic_z = 2
  31.  
  32.         # Reflection coefficients
  33.         self.b_x1 = 0.9
  34.         self.b_x2 = 0.92
  35.         self.b_y1 = 0.8
  36.         self.b_y2 = 0.84
  37.         self.b_z1 = 0.75
  38.         self.b_z2 = 0.92
  39.  
  40.     def simulate_room(self):
  41.         t = 1000
  42.  
  43.         # Generate x-planes depending on order
  44.         self.planes = list(range(-self.order, self.order+1))
  45.  
  46.         # Check for reflections - resolution depends on time (t)
  47.         for index in range(t):
  48.             self.check_for_reflexion(index)
  49.  
  50.         # Plot the calculated reflections
  51.         self.plot_reflections()
  52.  
  53.     def plot_reflections(self):
  54.         print(len(self.reflections[0]))
  55.         plt.stem(self.reflections[0], self.reflections[1], markerfmt=' ')
  56.         plt.title("Image Mirror Reflectogram - Order: " + str(self.order))
  57.         plt.xlabel('ms')
  58.         plt.ylabel('Gain')
  59.         plt.show()
  60.  
  61.     def check_for_reflexion(self, t):
  62.         for x in self.planes:
  63.             # To fit orders, y has to fit x_size - depends on order
  64.             y_size = abs(abs(x) - self.order)
  65.             y_planes = list(range(-y_size, y_size+1))
  66.  
  67.             for y in y_planes:
  68.                 # Same as y, but z is depending on both x and y
  69.                 if abs(abs(x) - self.order) and abs(abs(x) - self.order):
  70.                     if abs(y) > abs(x):
  71.                         z_size = abs(abs(y) - self.order)
  72.                     else:
  73.                         z_size = abs(abs(x) - self.order)
  74.                     z_planes = list(range(-z_size, z_size + 1))
  75.                 else:
  76.                     z_planes = [0]
  77.  
  78.                 for z in z_planes:
  79.                     # Calculations based on theory from "Image Method"
  80.                     Rp_x = (self.speaker_x - self.mic_x + 2 * x * self.mic_x)
  81.                     Rp_y = (self.speaker_y - self.mic_y + 2 * y * self.mic_y)
  82.                     Rp_z = (self.speaker_z - self.mic_z + 2 * z * self.mic_z)
  83.                     Rp = (Rp_x, Rp_y, Rp_z)
  84.                     Rt = (2 * x * self.Lx, 2 * y * self.Ly, 2 * y * self.Lz)
  85.                     delta = int(t - (np.linalg.norm(Rp + Rt) / self.c))
  86.  
  87.                     # Only trigger if delta is 1 - from definition
  88.                     if delta == 1:
  89.                         reflection = delta / 4 * math.pi * np.linalg.norm(Rp + Rt)
  90.                         self.reflections[0].append(t)
  91.                         self.reflections[1].append(self.calculate_absorption(x, y, z, reflection))
  92.  
  93.     def calculate_absorption(self, x, y, z, reflection):
  94.         # Calculations based on theory from "Image Method"
  95.         ref_x = (self.b_x1 ** abs(x)) * (self.b_x2 ** abs(x))
  96.         ref_y = (self.b_y1 ** abs(y)) * (self.b_y2 ** abs(y))
  97.         ref_z = (self.b_z1 ** abs(z)) * (self.b_z2 ** abs(y))
  98.         retval = ref_x * ref_y * ref_z
  99.         return retval
  100.  
  101.  
  102. if __name__ == '__main__':
  103.     obj = SpejlKilder()
  104.     obj.simulate_room()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement