I just came up with a little Frequency Harmonic Calculator in C++. Calculating harmonics isn’t hard, but if your doing a bunch of calculations, this might come in handy. To calculate a harmonic, simply take the fundamental frequency (F) and multiply it by an integer value (i.e. 3). So, for example, if F = 520 KHz, then the 3rd harmonic would be (Fx3) = (520×3) = 1560 KHz.

I also wrote a section of this program to calculate frequencies that have a harmonic of the frequency you enter. So if you enter 520, it will let you know that 260 KHz has a harmonic of 520 KHz. I needed something like this for a project I’m working on, so I decided to share what I came up with.

There is something funny/nice that occurs. If you enter both the value (i.e. 520) and the measurement (i.e. KHz) when it asks for the fundamental frequency, it will somehow write the frequency to the ‘freq’ variable and the measurement to the ‘unit’ variable and skip the step where I ask for the unit of measurement. All the calculations are correct and everything. I might know why this is, but I’m not sure enough to spit it out. Anyway, kinda cool.

I also put up this code here: http://pastebin.com/8y8pcVPr

#include <iostream> #include <stdio .h> #include <string> using namespace std; class calculator { public: int highArray[15]; int lowArray[15]; int freq; void harmonics(int); void lowFreq(int); }; void calculator::harmonics(int freq) { for (int i; i < = 15; i=i+1) { highArray[i] = freq*(i+1); } } void calculator::lowFreq(int freq) { for (int i=0; i<=15; i=i+1) { lowArray[i] = freq/(i+1); } } int main() { int freq; string unit; cout << "What is the fundamental frequency?\n\n"; cin >> freq; cout < < "\nWhat unit of measure are you useing? (Hz, KHz, MHz,... )\n\n"; cin >> unit; cout < < "\n\n"; calculator related_freqs; related_freqs.harmonics(freq); cout << "The harmonics of " << freq << " "; cout << unit << " are...\n\n"; for (int i=0; i<=15; i=i+1) { cout << "Harmonic #"<< (i+1) << " " << related_freqs.highArray[i]; cout << " " << unit << "\n"; } cout << "\n\n\nNow I'll calculate frequencies that have a harmonic of " << freq; cout << " " << unit << "\n\n"; related_freqs.lowFreq(freq); for (int i=0; i<=15; i=i+1) { cout << "Frequency #"<< (i+1) << " " << related_freqs.lowArray[i]; cout << " " << unit << "\n"; } cout << "\n\n\nPress any key and hit ENTER to quit.\n"; cin >> freq; // getchar(); // For some reason or another, this command has never worked for me. That's why I wrote the above line so that the user can actually read the data output. It's not as nice, but it works. return (0); }