# Optimization of Virtual Loudspeakers for Spatial Room Acoustics Reproduction with Headphones
## 2 Virtual loudspeaker Position Optimization
### Initialization and Weighted DOA Clustering
The repulsion area is gradually reduced to ensure that the algorithm converges even when there are lots of virtual loudspeakers to relocate. Finally, the optimization algorithm described above is summarized in Algorithm 1. First, the initial loudspeaker positions are sampled from the weight map that is generated from the weights (Section 2.3). The initial distances are controlled by rejecting the samples that are too close to previously sampled positions. When all the initial locations have been sampled successfully, the final positions are iterated by using weighted K-means clustering. Again, the distances of the iterated loudspeaker positions are monitored and loudspeakers with less weight are relocated in the case they come too close to more weighted ones. This relocation area is gradually reduced by the algorithm, which leads to convergence. The final result is then a set of virtual loudspeaker positions that are located close to the most weighted samples in their reproduction region. ```
1:functionOptimizeLoudspeakerPositions(\(\mathbf{w},\mathbf{u}_{\text{doa}},N_{\text{ls}},d_{\text{repulsion}}\))
2:\(\mathbf{W}_{\text{map}}\leftarrow\text{CalculateWeightMap}(\mathbf{w},\mathbf{u}_{\text{doa}})\)\(\triangleright\)Initialize loudspeaker positions
3:\(\mathbf{u}_{\text{ls}}\leftarrow\text{SampleWeightMap}(\mathbf{W}_{\text{map}},N_{ \text{ls}})\)\(\triangleright\)Calculate weighted K-means
4:repeat\(\triangleright\)Calculate weighted K-means
5:\(\mathbf{u}_{\text{ls},\text{old}}\leftarrow\mathbf{u}_{\text{ls}}\)
6:\(\mathbf{c}_{\text{ls}}\leftarrow\mathbf{0}^{N\times 1}\)
7:for\(n\gets 1\)to\(N\)do
8:\(\mathbf{c}_{\text{ls},n}\leftarrow\arg\min_{i}(||\mathbf{u}_{\text{ls},i}- \mathbf{u}_{\text{doa},n}||)\)\(\triangleright\) find the closest loudspeaker to the SDM data point
9:endfor
10:for\(i\gets 1\)to\(N_{\text{ls}}\)do
11:\((\mathbf{w}_{\text{cls}},\mathbf{u}_{\text{cls}})\leftarrow(\mathbf{w},\mathbf{u }_{\text{doa}})|_{\mathbf{c}_{\text{ls},n}=i}\)\(\triangleright\) assign the data point to the \(i\)th loudspeaker
12:\(\mathbf{u}_{\text{ls},i}\leftarrow\sum_{k}\left(\mathbf{w}_{\text{cls},k} \mathbf{u}_{\text{cls},k}\right)/\sum_{k}\left(\mathbf{w}_{\text{cls},k}\right)\)\(\triangleright\) weighted mean of the assigned DOAs
13:endfor
14:\(\mathbf{d}_{\text{closest}}\leftarrow\mathbf{0}^{N_{\text{ls}}\times 1}\)\(\triangleright\)Apply repulsion area
15:for\(i\gets 1\)to\(N_{\text{ls}}\)do
16:\(d_{\text{closest},i}\gets min_{j\
eq i}(||\mathbf{u}_{\text{ls},i}- \mathbf{u}_{\text{ls},j}||)\)\(\triangleright\) distance to the closest neighbor
17:endfor
18:for all \(d_{\text{closest},i}<d_{\text{repulsion}}\), from smallest to largest do
19:\(\mathbf{u}_{\text{v}}\leftarrow\text{vertices of a Voronoi diagram of }\mathbf{u}_{\text{ls}}\)\(\triangleright\) potential furthest points
20:\(\mathbf{u}_{\text{ls},i}\leftarrow\arg\max_{\mathbf{u}_{\text{v},j}}(\min_{i}(|| \mathbf{u}_{\text{v},j}-\mathbf{u}_{\text{ls},i}||))\)\(\triangleright\) Select the \(\mathbf{u}_{\text{v},j}\) furthest from all \(\mathbf{u}_{\text{ls}}\)
21:endfor
22: reduce \(d_{\text{repulsion}}\)
23:until all \(||\mathbf{u}_{\text{ls}}-\mathbf{u}_{\text{ls},\text{old}}||<\text{threshold}\)
24:return\(\mathbf{u}_{\text{ls}}\)
25:endfunction
```  
**Algorithm 1** Virtual loudspeaker optimization by using weighted DOA clustering.