Let us discuss something about the general solution of the Klein Gordon equation which will be later useful when we adopt for the field formulation of our theory.
Assuming our solution as a general waveform solution in the form,$$ \phi(x) = \frac{1}{(2\pi)^{2}}\int \,d^4k \tilde{\phi}(k) \,e^{-(ikx = ik_{\mu}x^{\mu})}$$ where 4 vector notation is generally implied unless it is specified.
We substitute this general form in our equation to obtain the conditions to be satisfied from the KG equation. It gives, $$ \frac{1}{(2\pi)^{2}}\int \,d^4k \,e^{-ikx}\left(-k^2+m^2\right)\tilde{\phi}(k) $$ [Normalization factor $\sqrt{2\pi}$ for four integrations].
From the above equation, the solution should be of the form such that, if $k^2=m^2$, $\tilde{\phi}(k)$ can take any arbitrary value and if $k^2\neq {m^2}$ then $\tilde{\phi}(k)$ should be zero,
therefore it should be in the form, $$\tilde{\phi}(k) = \delta(k^2-m^2)\,\tilde{f}(k)$$
Thus, our general wave solution cannot take any arbitrary form but should always satisfy the energy momentum relation - which is imposed by the dirac delta function. It is equivalent to saying that the energy can take only positive and negative values of $\omega_k$ given by $\delta(k^2-m^2) = \delta(k_0^2-|\vec{k}|^2-m^2) = \delta(k_0^2-\omega_k^2)$ where $\omega_k^2=|\vec{k}|^2+m^2$
Using the identity, $$\delta(f(x)) = \sum_{k}\frac{\delta(x-x_k)}{f'(x_k)}$$, we can rewrite our solution as,
$$\phi(x) = \frac{1}{(2\pi)^{2}}\int \,d^4k \left[e^{-ikx}\delta(k_0-\omega_k)\tilde{f}(k) + e^{-ikx}\delta(k_0+\omega_k)\tilde{f}(k)\right]$$ Now, integrating the zeroth component of momentum vector and using the fact that the integration is symmetric under $+\vec{k}$ and $-\vec{k}$ we can arrive at the form, $$\phi(x) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3k}{\sqrt{2\omega_k}} \left[e^{-ikx}\frac{\tilde{f}(\omega_k,\vec{k})}{\sqrt{4\pi\omega_k}}+ e^{ikx}\frac{\tilde{f}(-\omega_k,-\vec{k})}{\sqrt{4\pi\omega_k}}\right]$$
Now we identify the terms other than exponential as $ a(k), b^*(k)$ just for a general convenience. $$\phi(x) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3k}{\sqrt{2\omega_k}} \left[e^{-ikx} a(k) + e^{ikx} b^*(k)\right]\vert_{k_0=\omega_k} = \phi_+ +\phi_-$$
The complex conjugate is, $$\phi^*(x) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3k}{\sqrt{2\omega_k}} \left[e^{ikx} a^*(k) + e^{-ikx} b(k)\right]\vert_{k_0=\omega_k} $$
For real fields, one can show that, $a(k) = b(k)$ and $a^*(k) = b^*(k)$
Similarly, the momentum space terms can be written as, $$a(p) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3x}{\sqrt{2\omega_p}} \left[\omega_p \phi(x) +i\partial_0\phi(x)\right]e^{ipx}\vert_{p_0=\omega_p} $$ and $$b^*(p) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3x}{\sqrt{2\omega_p}} \left[\omega_p \phi(x) - i\partial_0\phi(x)\right]e^{ipx}\vert_{p_0=\omega_p}$$
Hint: One can prove from RHS to LHS by integrating with respect to $d^3x$, making use of the definition of Dirac delta function, integrating again with respect to $d^3k$ and finally using the fact that $\omega_k=\omega_{-k}=\omega_p=\omega_{-p}$ - the energy is independent of the direction of the momentum vector.
It is always a good practice to use different symbols to discuss it in a general form of the derivation.
Finally, one can obtain $a^*(p), b(p)$ by taking the complex conjugate.
Assuming our solution as a general waveform solution in the form,$$ \phi(x) = \frac{1}{(2\pi)^{2}}\int \,d^4k \tilde{\phi}(k) \,e^{-(ikx = ik_{\mu}x^{\mu})}$$ where 4 vector notation is generally implied unless it is specified.
We substitute this general form in our equation to obtain the conditions to be satisfied from the KG equation. It gives, $$ \frac{1}{(2\pi)^{2}}\int \,d^4k \,e^{-ikx}\left(-k^2+m^2\right)\tilde{\phi}(k) $$ [Normalization factor $\sqrt{2\pi}$ for four integrations].
From the above equation, the solution should be of the form such that, if $k^2=m^2$, $\tilde{\phi}(k)$ can take any arbitrary value and if $k^2\neq {m^2}$ then $\tilde{\phi}(k)$ should be zero,
therefore it should be in the form, $$\tilde{\phi}(k) = \delta(k^2-m^2)\,\tilde{f}(k)$$
Thus, our general wave solution cannot take any arbitrary form but should always satisfy the energy momentum relation - which is imposed by the dirac delta function. It is equivalent to saying that the energy can take only positive and negative values of $\omega_k$ given by $\delta(k^2-m^2) = \delta(k_0^2-|\vec{k}|^2-m^2) = \delta(k_0^2-\omega_k^2)$ where $\omega_k^2=|\vec{k}|^2+m^2$
Using the identity, $$\delta(f(x)) = \sum_{k}\frac{\delta(x-x_k)}{f'(x_k)}$$, we can rewrite our solution as,
$$\phi(x) = \frac{1}{(2\pi)^{2}}\int \,d^4k \left[e^{-ikx}\delta(k_0-\omega_k)\tilde{f}(k) + e^{-ikx}\delta(k_0+\omega_k)\tilde{f}(k)\right]$$ Now, integrating the zeroth component of momentum vector and using the fact that the integration is symmetric under $+\vec{k}$ and $-\vec{k}$ we can arrive at the form, $$\phi(x) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3k}{\sqrt{2\omega_k}} \left[e^{-ikx}\frac{\tilde{f}(\omega_k,\vec{k})}{\sqrt{4\pi\omega_k}}+ e^{ikx}\frac{\tilde{f}(-\omega_k,-\vec{k})}{\sqrt{4\pi\omega_k}}\right]$$
Now we identify the terms other than exponential as $ a(k), b^*(k)$ just for a general convenience. $$\phi(x) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3k}{\sqrt{2\omega_k}} \left[e^{-ikx} a(k) + e^{ikx} b^*(k)\right]\vert_{k_0=\omega_k} = \phi_+ +\phi_-$$
The complex conjugate is, $$\phi^*(x) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3k}{\sqrt{2\omega_k}} \left[e^{ikx} a^*(k) + e^{-ikx} b(k)\right]\vert_{k_0=\omega_k} $$
For real fields, one can show that, $a(k) = b(k)$ and $a^*(k) = b^*(k)$
Similarly, the momentum space terms can be written as, $$a(p) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3x}{\sqrt{2\omega_p}} \left[\omega_p \phi(x) +i\partial_0\phi(x)\right]e^{ipx}\vert_{p_0=\omega_p} $$ and $$b^*(p) = \frac{1}{(2\pi)^{3/2}}\int \,\frac{d^3x}{\sqrt{2\omega_p}} \left[\omega_p \phi(x) - i\partial_0\phi(x)\right]e^{ipx}\vert_{p_0=\omega_p}$$
Hint: One can prove from RHS to LHS by integrating with respect to $d^3x$, making use of the definition of Dirac delta function, integrating again with respect to $d^3k$ and finally using the fact that $\omega_k=\omega_{-k}=\omega_p=\omega_{-p}$ - the energy is independent of the direction of the momentum vector.
It is always a good practice to use different symbols to discuss it in a general form of the derivation.
Finally, one can obtain $a^*(p), b(p)$ by taking the complex conjugate.