0
$\begingroup$

I want to evaluate the code below for a series of s, running s from for example 1 to 100, or with s being a list of specific numbers in a list such as {5,7,10, etc}. I have attached a small sample of data.

The result for each evaluation, thus for each individual s in the series, should be exported into a separate column in a csv file.

In the attached notebook, as you see, I have set s to 5, thus exporting the resulting cosine projected 5000 steps into a single column csv. So far I have found the command Do:

Do[sums, {s, list}] or Do[sums, {s, 100}], but then I have trouble incorporating the successive export for each s calculated.

The code:

data = {4.423, 4.45, 4.45, 4.409, 4.423, 4.423, 4.423, 4.409, 4.437, 4.492, 4.492, 4.52, 4.52, 4.52, 4.52, 4.52, 4.506, 4.506, 4.52, 4.52, 4.52, 4.52, 4.464, 4.423, 4.381, 4.367, 4.367, 4.367, 4.311, 4.311, 4.27, 4.297, 4.381, 4.381, 4.381, 4.367, 4.395, 4.409, 4.423, 4.45, 4.45, 4.45, 4.409, 4.409, 4.409, 4.409, 4.381, 4.381, 4.381, 4.367, 4.381, 4.409, 4.381, 4.367, 4.367, 4.367, 4.381, 4.367, 4.395, 4.409, 4.409, 4.409, 4.409, 4.381, 4.367, 4.367, 4.395, 4.367, 4.367, 4.367, 4.339, 4.367, 4.353, 4.395, 4.409, 4.409, 4.409, 4.409, 4.395, 4.395, 4.395, 4.395, 4.395, 4.395, 4.381, 4.353, 4.339, 4.325, 4.284, 4.284, 4.284, 4.228, 4.255, 4.284, 4.284, 4.284, 4.284, 4.284, 4.284, 4.186, 4.228, 4.241, 4.255, 4.255, 4.255, 4.27, 4.27, 4.284, 4.284, 4.116, 4.116, 4.116, 4.144, 4.13, 4.116, 4.116, 4.116, 4.116, 4.116, 4.13, 4.144, 4.144, 4.144, 4.13, 4.13, 4.13, 4.13, 4.158, 4.144, 4.144, 4.158, 4.158, 4.158, 4.172, 4.186, 4.172, 4.172, 4.172, 4.172, 4.172, 4.158, 4.144, 4.116, 4.116, 4.116, 4.116, 4.116, 4.116, 4.101, 4.073, 4.059, 4.087, 4.087, 4.087, 4.087, 4.087, 4.087, 4.073, 4.087, 4.087, 4.087, 4.087, 4.087, 4.101, 4.116, 4.13, 4.13, 4.13, 4.144, 4.158, 4.172, 4.172, 4.172, 4.172, 4.172, 4.172, 4.158, 4.158, 4.172, 4.158, 4.158, 4.158, 4.158, 4.158, 4.172, 4.172, 4.144, 4.144, 4.144, 4.144, 4.172, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.214, 4.241, 4.255, 4.255, 4.255, 4.255, 4.255, 4.27, 4.27, 4.284, 4.284, 4.27, 4.27, 4.27, 4.264, 4.273, 4.343, 4.29, 4.273, 4.273, 4.273, 4.264, 4.238, 4.255, 4.203, 4.194, 4.194, 4.194, 4.212, 4.203, 4.194, 4.186, 4.186, 4.186, 4.186, 4.186, 4.177, 4.177, 4.186, 4.186, 4.186, 4.186, 4.203, 4.212, 4.221, 4.255, 4.255, 4.255, 4.255, 4.221, 4.255, 4.29, 4.29, 4.29, 4.29, 4.29, 4.36, 4.36, 4.378, 4.386, 4.325, 4.325, 4.325, 4.351, 4.333, 4.325, 4.273, 4.273, 4.273, 4.273, 4.264};

fs = Fourier[data];
n = Length[data];
s = {5};
a0f = Abs[fs[[1]]]/Sqrt[n];
af = Abs[fs][[s]];
pf = Arg[fs][[s]];
sums = Table[Sum[af[[j]]*Cos[(2*Pi*x*(s[[j]] - 1))/n - pf[[j]]], {j, 1, 
  Length[s]}] + a0f, {x, 0, 5000}];
export = Export["test.csv", sums];

Any help greatly appreciated!

Sincerely, Mike

$\endgroup$
1
  • $\begingroup$ Don't forget to hit the check mark beside any answers that answer your question, and hitting the up arrow to vote up answers. Welcome to MMA-SE! $\endgroup$ – morbo Nov 11 '20 at 23:02
1
$\begingroup$

Your question is I think a little unclear, but as far as I understand you're almost basically there, you simply need to expand your table some.

fs = Fourier[data];
n = Length[data];
s = Range[10];
a0f = Abs[fs[[1]]]/Sqrt[n];
af = Abs[fs][[s]];
pf = Arg[fs][[s]];


sums = Table[Sum[af[[j]]*Cos[(2*Pi*x*(s[[j]] - 1))/n - pf[[j]]], {j, 1, a}] + a0f, {x, 0, 5}, {a, 1, Length@s}]
export = Export["test.csv", sums];

This gives me a result of {6,10} values exported to a 6x10 csv...though, It's unclear if you want your sum to also increase as you increase the length of s for each subsequent value, however I assume this is the case.

$\endgroup$
4
  • $\begingroup$ Thanks. No - I want the result for each INDIVIDUAL s exported into their individual column, Using range I will ge the SUM of the combined cosine curve for 1-10. So far I have learnt that I could use Table[Table[... to get the part with different columns sorted - now I need the LOOP so that the calculation is done for each s. Perhaps I could delete the s = from the above code that use the Do[s...command? $\endgroup$ – Mike Nov 12 '20 at 6:03
  • $\begingroup$ Add: The code was designed to give the sum cosine curve so that part is obsolete for what I want right now - just to be clear. $\endgroup$ – Mike Nov 12 '20 at 6:12
  • $\begingroup$ Could you provide the table code for two different s values as seperate lines/codes/iterations, As the final calculations? I think maybe that’s help me understand ehat you’re looking for, your sum at the moment produces a constant value. $\endgroup$ – morbo Nov 12 '20 at 8:33
  • $\begingroup$ Thanks for the initiative. I got hold of an answer now. It involved to encapsulate the sum function for a specific value of s and nb of steps forward in a function then call the function in a table to generate the desired output. Full answer can be found @ Mathematica forum ( community.wolfram.com/groups/-/m/t/2113265?p_p_auth=6ShEULis ); $\endgroup$ – Mike Nov 13 '20 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.