-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
142 lines (89 loc) · 4.09 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import numpy as np
import h5py
import pandas as pd
# --- Sensor Setup ---
num_sensors = 10
radius = 0.03
sampling_frequency = 40e6
# --- ADC Settings ---
adc_resolution_bits = 12
adc_voltage_range = (-2.5, 2.5)
# --- Piezoelectric Sensor ---
sensor_sensitivity = 5e-12 # 5 pC/N
max_acoustic_pressure = 1e6
# --- Tissue Model ---
layer_properties = [
{'thickness': 0.02, 'speed_of_sound': 1540, 'attenuation': 0.5},
{'thickness': 0.05, 'speed_of_sound': 1580, 'attenuation': 0.8},
{'thickness': 0.08, 'speed_of_sound': 1570, 'attenuation': 0.6}
]
# --- Simulation ---
num_samples = 8192
reflector_depths = [0.04, 0.08, 0.14]
reflector_strengths = [0.8, 0.5, 0.3]
#(spherical coordinates)
theta = np.linspace(0, 2 * np.pi, num_sensors, endpoint=False)
phi = np.full_like(theta, 0.5 * np.pi) # Assuming half-circle
radius = 0.05
def quantize(data, bits, vmin, vmax):
resolution = 2**bits
step_size = (vmax - vmin) / resolution
quantized = np.round((data - vmin) / step_size).astype(int)
return quantized
def calculate_reflection_coefficient(z1, z2):
return (z2 - z1) / (z2 + z1)
def simulate_layered_reflections(pressure_wave):
interface_depths = np.cumsum([layer['thickness'] for layer in layer_properties])
current_depth = 0
for i, interface in enumerate(interface_depths[:-1]): # Change here
distance_to_interface = interface - current_depth
travel_time = 2 * distance_to_interface / layer_properties[i]['speed_of_sound']
delay = int(travel_time * sampling_frequency)
R = calculate_reflection_coefficient(layer_properties[i]['speed_of_sound'], layer_properties[i+1]['speed_of_sound'])
pressure_wave[delay:] += R * pressure_wave[delay:]
attenuation_factor = np.exp(-layer_properties[i]['attenuation'] * distance_to_interface * sampling_frequency * 1e-6)
pressure_wave *= attenuation_factor
current_depth = interface
return pressure_wave
# Simulate sensor readings
sensor_data = np.zeros((num_sensors, num_samples))
interface_depths = np.cumsum([layer['thickness'] for layer in layer_properties])
for i, depth in enumerate(reflector_depths):
pressure_wave = reflector_strengths[i] * max_acoustic_pressure * np.random.randn(num_samples)
pressure_wave = simulate_layered_reflections(pressure_wave)
voltage_signal = sensor_sensitivity * pressure_wave
# Calculate delay
layer_index = next(x[0] for x in enumerate(interface_depths) if x[1] > depth)
travel_time = 2 * depth / layer_properties[layer_index]['speed_of_sound']
delay = int(travel_time * sampling_frequency)
if delay < num_samples: # Check if delay is within bounds
voltage_signal[delay:] += voltage_signal[delay]
sensor_data[:, delay:] += quantize(voltage_signal[delay:], adc_resolution_bits, *adc_voltage_range)
else:
print(f"Warning: Echo from reflector at {depth} arrives beyond signal length - Skipping")
voltage_signal[delay:] += voltage_signal[delay]
sensor_data[:, delay:] += quantize(voltage_signal[delay:], adc_resolution_bits, *adc_voltage_range)
# --- Metadata ---
metadata = {
'sampling_frequency_hz': sampling_frequency,
'speed_of_sound_mps': 1540,
'sensor_array_type': 'uniform_circular',
'num_sensors': num_sensors,
}
# --- HDF5 Creation ---
with h5py.File('ultrasound_data.h5', 'w') as hf:
hf.create_dataset('sensor_data', data=sensor_data)
# Combine sensor positions
sensor_positions = np.vstack((theta, phi, radius)).T
hf.create_dataset('sensor_positions', data=sensor_positions)
hf.create_dataset('timestamps', data=np.arange(num_samples) / sampling_frequency)
for key, value in metadata.items():
hf.attrs[key] = value
# --- CSV Creation ---
df = pd.DataFrame(sensor_data.T)
timestamps = np.arange(num_samples) / sampling_frequency
df['timestamp'] = timestamps
df.to_csv('ultrasound_data.csv', index=False, header=False)
# ... (Your existing code)
# ... Your code to calculate sensor positions
# Example placeholder (spherical coordinates) - replace this!