# Combine single pulse with Boolean test, and integrate series

I have edited this to include integration:

This is a more general case of this question.

I want to create a function that gives the integral of a series of single pulses of the form Cos[Pi*(x/2)]^2 for all values of x that satisfy some Boolean test.

For example, say I wanted to create a curve that summed pulses for all instances of x on the Fibonacci sequence. Manually, I would do something like

Plot[{With[{n = 1}, Cos[Pi*(Clip[x - n]/2)]^2] +
With[{n = 2}, Cos[Pi*(Clip[x - n]/2)]^2] +
With[{n = 3}, Cos[Pi*(Clip[x - n]/2)]^2] +
With[{n = 5}, Cos[Pi*(Clip[x - n]/2)]^2] +
With[{n = 8}, Cos[Pi*(Clip[x - n]/2)]^2] +
With[{n = 13}, Cos[Pi*(Clip[x - n]/2)]^2]}, {x, 0, 15}] Integrating (how?) would create a continuous curve.

That's just an example. I want a function in the form Integral[booleantest*pulse,x] that gives the cumulative area under a series of this style of pulse, where each pulse is centred on any Boolean test on integer values of x.

So it seems to me that my question has three parts:

1. How do I create a function pulse that gives this pulse-form for all integer x? Clearly using With[{n=...,] isn't going to work. It requires me to manually specify all instances of pulse.

2. In principle, it's easy enough to create a Boolean test: booleantest=Boole[EvenQ[x/3]], booleantest=Boole[PrimeQ[x]], etc.

So, how do I multiply the two together? booleantest*pulse should yield pulse for all instances of booleantest=1, and 0 else. But it doesn't work. For example: pulse = Cos[Pi*(Clip[x - n]/2)]^2;booleantest = Boole[EvenQ[n/3]];Plot[pulse*booleantest, {n, 0, 15}] 1. How do I create the integral?

I'm clearly doing lots of things wrong... So, sticking to the idea of pulse*booleantest (if possible), how do I do this?

UPDATE

Many thanks to @kglrfor the answer below. I have now tried to expand this to a more general Boolean choice, as per original post. I'm ashamed to say that I can't figure out how do it. @kglr, your suggestion works great for the Fibonacci sequence. But how would it work for, for example, with EvenQ[x/n]=True? Or 'FreeQ[{x},n]? Or *any* of a host of other 'filters' of this generalQ nature? It's the general approach that I'm after.

Update: A function that takes an arbitrary list of integers as the second argument:

ClearAll[f2]
f2[x_?NumericQ, m : {__Integer}] := Total[Cos[Pi*(Clip[x - #]/2)]^2 & /@ m]
Grid[Partition[Plot[f2[x, #], {x, -1 + Min@#, 1 + Max@#}, PlotRange -> {0, 2},
ImageSize -> 300, PlotLabel -> Style["m = " <> ToString[#], 16]] & /@
{Fibonacci[Range@7], Select[Range[-5, 5], EvenQ],
Select[Range[-5, 5], OddQ], Select[Range[-15, 15], Divisible[#, 6] &],
{-11, -10, -9, -2,  1,  5, 6}, RandomSample[Range[-15, 15], 6]}, 3],
Dividers -> All] ClearAll[f1]
f1[x_?NumericQ, n_Integer] := Total[Cos[Pi*(Clip[x - #]/2)]^2 & /@ Fibonacci[Range[n]]]
Grid[Partition[Plot[f1[x, #] , {x, 0, 20}, PlotStyle -> ColorData[#],
PlotLabel -> Style["n = " <> ToString[#], 16],
PlotRange -> {0, 2}, ImageSize -> 200] & /@ Range, 3],
Dividers -> All] Plot[Evaluate[f1[x, #] & /@ Range], {x, 0, 20},
PlotRange -> {0, 2}, PlotLegends -> Range, ImageSize -> 500] ClearAll[f2]
f2[x_?NumericQ, n_Integer] := NIntegrate[f1[s, n], {s, 0, x}]
ListLinePlot[Table[{x, f2[x, 7]}, {x, 0, 15, 1/100}]]
` • Fantastic. Thanks @kglr :-) – Richard Burke-Ward Oct 23 '18 at 8:31
• Hi all (especially @kglr). Please see clarification in my edit to the original post above. The solution offered works for the Fibonacci series, but not in a more general context... So, I'm very grateful but would also appreciate further input. – Richard Burke-Ward Oct 24 '18 at 11:38
• Many thanks for the updated answer. Perfect! – Richard Burke-Ward Oct 24 '18 at 12:17