Thank your again for your time!
I specially like the strategy you use to asure that for each frequency we catch an integer number of cycles! It's a very good way.
I think that the program you have attached works well, the only thing is that you did a final sum instead of doing a mean. I haven't still tested it with experimental data, but with the simulated signal it works.
I attach your program with these small modifications.