## 2 Methodology
### Watermark Embedding
#### 2.3.2 Optimizing the Embedding Process  
To embed a watermark in an image, we should first select frequency domain coefficients from the decomposed host image. Usually, this process is applied sequentially on the coefficients. However, in this research, we will use the particle swarm intelligence algorithm to optimize the selection of the coefficients. The optimization algorithm fitness function is used for minimizing degradation in the watermarked image. PSO is a type of stochastic optimization algorithm and its process is inspired by the social interaction behavior of flocking birds or schooling fish, which differs significantly from traditional individual cognitive elements. This algorithm consists of a population of candidate solutions (particles) which exist in the search space and are given a fitness value for the maximization function. The PSO algorithm focuses on the purpose of minimizing or maximizing the objective function. All particles update their position based on their own experience (cognitive behavior) and development, as well as the best location reached so far by all other particles (social behavior). For a brief overview of the PSO algorithm, the methodology was introduced in [30]. Each particle in the swarm consists of three 3-dimensional vectors, the current position (\(x_{i}\)), the best position reached by that particle (\(pbest_{i}\)), and the velocity (\(v_{i}\)), as shown in Equation (3). The velocity vector (\(v_{i}\)) controls the optimization process and enables information exchange between particles as well as utilizing the historical knowledge of each particle, as given in Equation (4). By adding the velocity coordinates (\(v_{i}\)) to the current position (\(x_{i}\)), the updated position of each particle (\(x^{\prime}_{i}\)) is calculated. \[v_{i}=\omega\ v_{i}+C_{1}\varphi_{1}\left(p_{besti}-x_{i}\right)+C_{2}\varphi_{2 }(g_{best}-x_{i}) \tag{3}\]  
\[x^{\prime}_{i}=x_{i}+v_{i} \tag{4}\]  
where inertia weight (\(\omega\)) is a constant that determines the balance between global and local search, while \(\varphi\)1 and \(\varphi\)2 represent uniformly distributed random values within [0, 1]. Additionally, positive acceleration constants C1 and C2 are used to scale the contribution of cognitive and social components, respectively, in the particle's movement. As an example, suppose that a particle's current velocity is \(v_{i}=2\), its personal best is at position 5, and the global best is at position 8. If the inertia weight is \(\omega=0.7\) and \(C_{1}\) and \(C_{2}\) are both set to 1.5, with random values \(\varphi_{1}=0.9\) and \(\varphi_{2}=0.7\), the updated velocity would be  
\[v_{i}=0.7\left(2\right)+1.5\left(0.9\right)\left(5-x_{i}\right)+1.5\left(0.7 \right)\left(8-x_{i}\right)\]  
This gives the new velocity based on how far the particle is from its personal best and the global best, allowing it to move closer to an optimal solution. PSO is applied to the coefficients obtained from the decomposition step. The pseudocode for finding the optimal set of coefficients using PSO is shown in Algorithm A1 (Appendix A). As the algorithm shows, the coefficients are divided into \(m\) swarms, where each swarm has several particles. Each coefficient in a swarm is considered as a particle. The objective function is calculated over each particle in each swarm, and the best value in swarm \(i\) is denoted as \(pbest_{i}\), while the best objective function over all swarms is considered as \(gbest\). The inertia is calculated for each iteration and used as a weight contribution of the previous velocity. The updated velocity is computed from the weighted velocity in the previous iteration, along with the distance of particle \(i\) from the \(pbesti\) and the distance of particle \(i\) from \(gbest\). After finding out the velocity of particle \(i\), we can generate a random number from [0, 1] and compare the obtained number with the sigmoid value of the new velocity.