# How to group terms of a Fourier series properly?

I want to organize my expressions as a fourier series of sin and cos. That is, I want them to be of the form $$a_0 + a_1 \cos[ w t] + b_1 \sin[ w t] + a_2 \cos[2 w t] + b_2 \sin[2 w t] + ...$$

Unfortunately, the expressions are messy. Terms in the expressions contain products and sums of time-sinusoids and other sinusoids, such as $$(\sin[3 w t+ \text{expr}]^2 +\cos[ \text{expr}_2])^3 \cos[ \text{expr}_3]$$

# What I tried

I would like to use organize them into terms of varying frequencies (e.g. Sin[n w t] , where n is an integer)

• I tried Collect[%, {Cos[_*w*t], Sin[_*w*t]}, Together], however, this doesn't work because I have products of time sinusoides that I need to trig reduce.

• I tried using trig reduce, but that mixes the time-sinusoids with the other sinusoids.

• I tried a variety of permutations involving trig reduce, trig expand, expand, and Collect[%, {Cos[w*t]^_, Sin[_*w*t]^_}, Together], to no avail.

• I tried explicitly defining rules to apply before applying Collect, such as

trigNoPower = {
Cos[ n_]^a_ ?(IntegerQ[#] && # > 1 &) ->
Cos[  n]^(a - 2)   * (1 + Cos[2 n])/2 ,
Sin[ n_]^a_ ?(IntegerQ[#] && # > 1 &) ->
Cos[  n]^(a - 2)   * (1 - Cos[2 n])/2 ,

Sin[a_] Sin[b_] -> (Cos[a - b] - Cos[a + b])/2,
Cos[a_] Cos[b_] -> (Cos[a - b] + Cos[a + b])/2,
Sin[a_] Cos[b_] -> (Sin[a + b] + Sin[a - b])/2,
Cos[a_] Sin[b_] -> (Sin[a + b] - Sin[a - b])/2
};
(*Example Usage*)
(4 BesselJ[2, \[Phi]m]^2 Cos[2 t w]^2 Sin[\[Delta]m] Sin[\[Delta]p])/(
rm rp)  //. trigNoPower;
Collect[%, {Cos[_*w*t], Sin[_*w*t]}, HoldForm@*Evaluate@*Simplify]

However no combination of the above worked completely for my sample expressions below. What should I do instead?

# Sample Expressions

For reference, one of my expressions is:

hopefullyFourier = 4 ξ^2 ((BesselJ[0, ϕm] +
2 BesselJ[2, ϕm] Cos[2 t w])^2 Sin[δp]^2 +
4 βq^2 Cos[ϕmag]^2 (BesselJ[1, ϕm] Sin[t w] +
BesselJ[3, ϕm] Sin[
3 t w])^2 Sin[θmag]^4 Sin[ϕmag]^2)

or expanded

In[1]:= hopefullyFourier //Expand
Out[1] = 4 ξ^2 BesselJ[0, ϕm]^2 Sin[δp]^2 +
16 ξ^2 BesselJ[0, ϕm] BesselJ[2, ϕm] Cos[
2 t w] Sin[δp]^2 +
16 ξ^2 BesselJ[2, ϕm]^2 Cos[2 t w]^2 Sin[δp]^2 +
16 βq^2 ξ^2 BesselJ[1, ϕm]^2 Cos[ϕmag]^2 Sin[
t w]^2 Sin[θmag]^4 Sin[ϕmag]^2 +
32 βq^2 ξ^2 BesselJ[1, ϕm] BesselJ[
3, ϕm] Cos[ϕmag]^2 Sin[t w] Sin[
3 t w] Sin[θmag]^4 Sin[ϕmag]^2 +
16 βq^2 ξ^2 BesselJ[3, ϕm]^2 Cos[ϕmag]^2 Sin[
3 t w]^2 Sin[θmag]^4 Sin[ϕmag]^2

And to give another taste, that includes how I generate a particular messy expression (i.e. hopefullyFourier2 below) from a Jacobi-Anger expansion

jacobiAnger[
order_] = {Cos[z_*Sin[\[Theta]_]] ->
BesselJ[0, z] +
2 Sum[BesselJ[n, z] Cos[n \[Theta]], {n, 2, order, 2}],
Sin[z_*Sin[\[Theta]_]] ->
2 Sum[BesselJ[n, z] Sin[n \[Theta]], {n, 1, order, 2}]
};

faradayIntensity = 1/(2 rm^2) + 1/(2 rp^2) + Cos[\[Delta]m - \[Delta]p]/(rm rp) +
Cos[2 \[Phi]m Sin[t w]]/(2 rm^2) + Cos[2 \[Phi]m Sin[t w]]/(2 rp^2) +
Cos[\[Delta]m - \[Delta]p - 2 \[Phi]m Sin[t w]]/(2 rm rp) +
Cos[\[Delta]m - \[Delta]p + 2 \[Phi]m Sin[t w]]/(2 rm rp)
% // TrigExpand;
hopefullyFourier2 = % /. jacobiAnger[3];

% // Expand;
% //. trigNoPower; (* see the what I tried section for definition of this *)
Collect[%, {Cos[_*w*t], Sin[_*w*t]}, Hold@*Evaluate@*Simplify];

$$`$$