Since you're doing all the work only to return coherence, look at
to return the coherence directly with all the intermediares buried in the call instead of separate.
Alternatively, you could use the array syntax of cpsd and pass arrays instead of three calls with vectors--
[P,freq]=cpsd([f1 f2 f1],[f1 f2 f2],hanning(512),,512,500);
The burying of "magic numbers" in code makes things hard to change/debug; if that number would change, you have to change the code and find all occurrences to ensure everything changes together. If it were a variable, then only change data value and (hopefully) the variable is used everywhere.
Of course, this value and the inevitable comparison to Deep Thought and The Hitchhiker's Guide to the Galaxy, is purely coincidental, I'm sure.