How to visualize the frequency spectrum of a signal.

I have tried the following:
i write a signal to an array of 1024 points (cosine wave of osc~)
then i read it back with tabread~
do an rfft~
then i try to get the magnitude response from this by squaring both outputs, summing them, and then sqrt~'ing that sum.
And then use a tabwrite to another array (1024 points) to visualize it. But i don't seem to get satisfactory results.

Maybe there is a better way to do it?

i added my pd file as an attachment.

EDIT: added updated fixed patch

http://www.pdpatchrepo.info/hurleur/magnitude_spectrum.pd