|
3 | 3 | from pytARD_2D.interface import InterfaceData2D, Direction2D
|
4 | 4 |
|
5 | 5 | from common.parameters import SimulationParameters
|
6 |
| -from common.impulse import Gaussian, Unit, WaveFile |
7 |
| -from common.serializer import Serializer |
8 |
| -from common.plotter import Plotter |
9 |
| -from common.microphone import Microphone as Mic |
| 6 | +from common.impulse import Unit |
10 | 7 |
|
11 | 8 | import numpy as np
|
12 |
| -from datetime import date, datetime |
13 | 9 | from pycallgraph import PyCallGraph
|
14 | 10 | from pycallgraph.output import GraphvizOutput
|
15 | 11 | from pycallgraph import GlobbingFilter
|
16 | 12 | from pycallgraph import Config
|
17 | 13 |
|
18 |
| - |
19 |
| -# Simulation parameters |
20 |
| -duration = 1 # seconds |
21 |
| -Fs = 8000 # sample rate |
22 |
| -upper_frequency_limit = 200 # Hz |
23 |
| -c = 342 # m/s |
24 |
| -spatial_samples_per_wave_length = 6 |
25 |
| - |
26 |
| -# Procedure parameters |
27 |
| -verbose = True |
28 |
| -auralize = True |
29 |
| -visualize = True |
30 |
| -write_to_file = False |
31 |
| - |
32 |
| -# Compilation of room parameters into parameter class |
| 14 | +''' |
| 15 | +Utility script to create call graphs with PyCallGraph. |
| 16 | +''' |
| 17 | +c = 342 |
33 | 18 | sim_param = SimulationParameters(
|
34 |
| - upper_frequency_limit, |
35 |
| - duration, |
| 19 | + 200, |
| 20 | + 1, |
36 | 21 | c=c,
|
37 |
| - Fs=Fs, |
38 |
| - spatial_samples_per_wave_length=spatial_samples_per_wave_length, |
39 |
| - verbose=verbose, |
40 |
| - visualize=visualize |
| 22 | + Fs=8000, |
| 23 | + spatial_samples_per_wave_length=6, |
| 24 | + verbose=False, |
| 25 | + visualize=False |
41 | 26 | )
|
42 | 27 |
|
43 |
| -# Location of impulse that gets emitted into the room. |
44 |
| -impulse_location = np.array([[2], [2]]) |
45 |
| - |
46 |
| -# Define impulse that gets emitted into the room. Uncomment which kind of impulse you want. |
47 |
| -#impulse = Gaussian(sim_param, impulse_location, 10000) |
48 |
| -impulse = Unit(sim_param, impulse_location, 1, 100) |
49 |
| -#impulse = WaveFile(sim_param, impulse_location, 'clap_48000.wav', 1000) |
50 |
| - |
51 |
| -# Damping profile with according Zetta value (how much is absorbed) |
52 |
| -dp = DampingProfile(4, c, 1e-8) |
53 |
| - |
54 |
| -partitions = [] |
55 |
| -# Paritions of the room. Can be 1..n. Add or remove partitions here. |
56 |
| -# This example is a square room surrounded by absorbing PML partitions. |
57 |
| -# Also, you may provide impulse in the partition(s) of your choosing. |
58 |
| -partitions.append(AirPartition2D(np.array([[4.0], [4.0]]), sim_param, impulse=impulse)) |
59 |
| -partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp)) |
60 |
| -#partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp)) |
61 |
| -#partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp)) |
62 |
| -#partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp)) |
63 |
| - |
64 |
| -# Interfaces of the room. Interfaces connect the partitions together |
65 |
| -interfaces = [] |
66 |
| -interfaces.append(InterfaceData2D(1, 0, Direction2D.X)) |
67 |
| -#interfaces.append(InterfaceData2D(2, 0, Direction2D.X)) |
68 |
| -#interfaces.append(InterfaceData2D(3, 0, Direction2D.Y)) |
69 |
| -#interfaces.append(InterfaceData2D(4, 0, Direction2D.Y)) |
70 |
| - |
71 |
| -# Microphones. Add and remove microphones here by copying or deleting mic objects. |
72 |
| -# Only gets used if the auralization option is enabled. |
73 |
| -if auralize: |
74 |
| - mics = [] |
75 |
| - mics.append(Mic( |
76 |
| - 0, # Parition number |
77 |
| - # Position |
78 |
| - [ |
79 |
| - int(partitions[0].dimensions[0] / 1.5), |
80 |
| - int(partitions[0].dimensions[1] / 1.5) |
81 |
| - ], |
82 |
| - sim_param, |
83 |
| - # Name of resulting wave file |
84 |
| - f"pytARD_2D_{date.today()}_{datetime.now().time()}" |
85 |
| - )) |
| 28 | +partitions = [ |
| 29 | + AirPartition2D(np.array([[4.0], [4.0]]), sim_param, impulse=Unit(sim_param, np.array([[2], [2]]), 1, 100)), |
| 30 | + PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, DampingProfile(4, c, 1e-8)) |
| 31 | +] |
86 | 32 |
|
87 |
| -# Instantiation serializer for reading and writing simulation state data |
88 |
| -serializer = Serializer() |
| 33 | +interfaces = [ |
| 34 | + InterfaceData2D(1, 0, Direction2D.X) |
| 35 | +] |
89 | 36 |
|
90 | 37 | # Instantiating and executing simulation
|
91 | 38 | config = Config()
|
92 | 39 | config.trace_filter = GlobbingFilter(exclude=[
|
93 |
| - 'tqdm.*', |
94 |
| - '*.tqdm', |
95 |
| - 'tqdm', |
96 |
| - 'pycallgraph.*', |
97 |
| - '*.secret_function', |
98 |
| - 'multiprocessing', |
99 |
| - '*.multiprocessing', |
100 |
| - 'multiprocessing.*' |
| 40 | + 'tqdm.*', '*.tqdm', 'tqdm', 'pycallgraph.*', '*.secret_function', 'multiprocessing', '*.multiprocessing', 'multiprocessing.*' |
101 | 41 | ])
|
102 | 42 |
|
103 | 43 | with PyCallGraph(output=GraphvizOutput(), config=config):
|
104 |
| - |
105 |
| - sim = ARDSimulator2D(sim_param, partitions, 1, interfaces, mics) |
| 44 | + sim = ARDSimulator2D(sim_param, partitions, 1, interfaces) |
106 | 45 | sim.preprocessing()
|
107 | 46 | sim.simulation()
|
108 |
| - |
109 |
| -# Find best peak to normalize mic signal and write mic signal to file |
110 |
| -if auralize: |
111 |
| - def find_best_peak(mics): |
112 |
| - peaks = [] |
113 |
| - for i in range(len(mics)): |
114 |
| - peaks.append(np.max(mics[i].signal)) |
115 |
| - return np.max(peaks) |
116 |
| - |
117 |
| - all_mic_peaks = [] |
118 |
| - all_mic_peaks.append(find_best_peak(mics)) |
119 |
| - best_peak = np.max(all_mic_peaks) |
120 |
| - |
121 |
| - def write_mic_files(mics, peak): |
122 |
| - for i in range(len(mics)): |
123 |
| - mics[i].write_to_file(peak, upper_frequency_limit) |
124 |
| - |
125 |
| - write_mic_files(mics, best_peak) |
126 |
| - |
127 |
| -# Structure of plot graph. Optional, only for visualization. |
128 |
| -plot_structure = [ |
129 |
| - # Structure: [Width of domain, height of domain, index of partition to plot on the graph (min: 1, max: width*height)] |
130 |
| - [3, 3, 5], |
131 |
| - [3, 3, 4], |
132 |
| - [3, 3, 6], |
133 |
| - [3, 3, 2], |
134 |
| - [3, 3, 8] |
135 |
| -] |
136 |
| - |
137 |
| -# Write partitions and state data to disk |
138 |
| -if write_to_file: |
139 |
| - serializer.dump(sim_param, partitions, mics, plot_structure) |
140 |
| - |
141 |
| -# Plotting waveform |
142 |
| -if visualize: |
143 |
| - plotter = Plotter() |
144 |
| - plotter.set_data_from_simulation(sim_param, partitions, mics, plot_structure) |
145 |
| - plotter.plot(enable_colorbar=True) |
0 commit comments