Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import math
- import matplotlib.pyplot as plt
- class SpejlKilder:
- def __init__(self):
- self.frequency = 1000
- self.c = 0.34
- self.i = (-1) ** (1 / 2)
- self.planes = []
- self.reflections = [[], []]
- # Mirror image order
- self.order = 3
- # Room dimenstions
- self.Lx = 4
- self.Ly = 5
- self.Lz = 3
- # Speaker placement
- self.speaker_x = 2
- self.speaker_y = 3
- self.speaker_z = 1
- # Listener Placement
- self.mic_x = 1
- self.mic_y = 3
- self.mic_z = 2
- # Reflection coefficients
- self.b_x1 = 0.9
- self.b_x2 = 0.92
- self.b_y1 = 0.8
- self.b_y2 = 0.84
- self.b_z1 = 0.75
- self.b_z2 = 0.92
- def simulate_room(self):
- t = 1000
- # Generate x-planes depending on order
- self.planes = list(range(-self.order, self.order+1))
- # Check for reflections - resolution depends on time (t)
- for index in range(t):
- self.check_for_reflexion(index)
- # Plot the calculated reflections
- self.plot_reflections()
- def plot_reflections(self):
- print(len(self.reflections[0]))
- plt.stem(self.reflections[0], self.reflections[1], markerfmt=' ')
- plt.title("Image Mirror Reflectogram - Order: " + str(self.order))
- plt.xlabel('ms')
- plt.ylabel('Gain')
- plt.show()
- def check_for_reflexion(self, t):
- for x in self.planes:
- # To fit orders, y has to fit x_size - depends on order
- y_size = abs(abs(x) - self.order)
- y_planes = list(range(-y_size, y_size+1))
- for y in y_planes:
- # Same as y, but z is depending on both x and y
- if abs(abs(x) - self.order) and abs(abs(x) - self.order):
- if abs(y) > abs(x):
- z_size = abs(abs(y) - self.order)
- else:
- z_size = abs(abs(x) - self.order)
- z_planes = list(range(-z_size, z_size + 1))
- else:
- z_planes = [0]
- for z in z_planes:
- # Calculations based on theory from "Image Method"
- Rp_x = (self.speaker_x - self.mic_x + 2 * x * self.mic_x)
- Rp_y = (self.speaker_y - self.mic_y + 2 * y * self.mic_y)
- Rp_z = (self.speaker_z - self.mic_z + 2 * z * self.mic_z)
- Rp = (Rp_x, Rp_y, Rp_z)
- Rt = (2 * x * self.Lx, 2 * y * self.Ly, 2 * y * self.Lz)
- delta = int(t - (np.linalg.norm(Rp + Rt) / self.c))
- # Only trigger if delta is 1 - from definition
- if delta == 1:
- reflection = delta / 4 * math.pi * np.linalg.norm(Rp + Rt)
- self.reflections[0].append(t)
- self.reflections[1].append(self.calculate_absorption(x, y, z, reflection))
- def calculate_absorption(self, x, y, z, reflection):
- # Calculations based on theory from "Image Method"
- ref_x = (self.b_x1 ** abs(x)) * (self.b_x2 ** abs(x))
- ref_y = (self.b_y1 ** abs(y)) * (self.b_y2 ** abs(y))
- ref_z = (self.b_z1 ** abs(z)) * (self.b_z2 ** abs(y))
- retval = ref_x * ref_y * ref_z
- return retval
- if __name__ == '__main__':
- obj = SpejlKilder()
- obj.simulate_room()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement