\begin{align}
f(0) &= 1 \\
f(1) &= 2 \\
f(n) &= 2f(n-1)-f(n-2)+\sigma_0(n-1) \\
&= 1+n+\sum_{i=1}^{n-1}i\cdot\sigma_0(n-i)
\end{align}
Where $\sigma_0$ is the divisor count, A000005 on OEIS. I don't have a full formal proof, but I can sketch it.
Instead of looking at all possible $P$ and $T$, we're going to look directly at possible matching sequences $M$ to enumerate those. $M$ is always a possibility if it's all zeros (consider $P$ all ones, $T$ all zeros). $M$ is also always a possibility if it contains exactly one $1$ (consider $P$ all ones, $T$ all zeros except a run of $n$ ones in the right position).
What if $M$ contains multiple $1$s? If $M$ contains two $1$s which are $k$ positions apart, then all $P$ that can lead to $M$ have to have period $k$. This is easy to see if we look at an example. Consider $M=??1?1???$, where the $?$ are arbitrary, i.e. $k=2$. We have $P=abcdefgh$, where the $a$ to $h$ are (independently) $0$ or $1$. Those two $1$s in $M$ impose a certain structure on $T$:
M ??101???
T = ??abcdefgh????? imposed by first 1
T = ????abcdefgh??? imposed by second 1
From this, we can see that $a=c=e=g$ and $b=d=f=h$ and hence $P$ needs to have period $2$.
Now since $T$ has length $2n-1$ all the constraints imposed by the $1$s in $M$ overlap in at least one position. Hence, the segment between the first $1$ and the last $1$ has to obey the same period. In other words, the distance between any two adjacent $1$s in a valid $M$ must be the same. Valid examples include $111$ and $000100100100000$, but not $1101$, $010010001$ or $010101000101$. Smylic proves this formally in their answer.
With this in mind, we can construct either a recursive or an explicit formula for the number of valid $M$, $f(n)$. Let's have a look at the full list for $n=4$:
$$
0000\\
0001\\
0010\\
0011\\
0100\\
0101\\
0110\\
0111\\
1000\\
1001\\
1010\\
1100\\
1110\\
1111\\
$$
We can always generate a valid $M_n$ (i.e. a matching sequence of length $n$) by taking an $M_{n-1}$ and prepending a zero. That gives us the first eight $M_4$ in the list above. Their number if of course $f(n-1)$.
We can also generate a valid $M_n$ by appending a zero, but we need to make sure that we don't double-count with the previous step. The $M_{n-1}$ which we can append a zero to and obtain a new $M_n$ are those that start with a $1$. In otherwords, those that weren't obtained from prepending a zero. There are four of these for $M_4$ and their general number is $f(n-1) - f(n-2)$.
Finally, there are some $M_n$ that start and end with $1$. Since we're adding a new $1$, we need to make sure that their distances are all the same. But this is quite easy since we know that the $1$s span the entire $n$ positions. This means that there must be a $1$ in every $j$th position, where $j$ divides $n-1$ (e.g. $1001001$ where there's a $1$ in every third position and $3$ divides $n-1=6$). The number of ways we can write down these ones are the number of divisors of $n-1$, $\sigma_0(n-1)$.
Taking that all together we arrive at the above recursive formula
$$
f(n) = 2f(n-1)-f(n-2)+\sigma_0(n-1)
$$
Alternatively, we can look at it explicitly: like I said, having all $0$s always works, which is one possibility. Having a single $1$ always works, which gives us $n$ possibilities. If there is more than one $1$, the $1$s span some substring of $M$ of length $2\leq i\leq n$. We can treat this substring the same way as the last case for the recursive derivation and find that there are $\sigma_o(i-1)$ ways to place the $1$s in that substring. Additionally, there are $n+1-i$ to place that substring into $M$, surrounded by $0$s, which gives us the multiplicity in the sum:
$$
f(n) = 1+n+\sum_{i=1}^{n-1}i\cdot\sigma_0(n-i)
$$