/** FMSpectrumCalculator Calculate the spectrum of a simple FM pair. * @author Phil Burk and Nick Didkovsky */ /* * (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved * JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom. */ package jmsltutorial; import java.util.Vector; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.util.Bessel; public class FMSpectrumCalculator { Vector spectrum; // contains a bunch of SpectrumDatum /* calculated as modAmp / modFreq */ double indexOfModulation = 1.0; /* expressed in Hertz */ double modAmp = 0.0; /* expressed in Hertz */ double modFreq = 1000.0; /* expressed in Hertz */ double carrierFreq = 10000.0; /* 0.0 .. 1.0 */ double carrierAmp = 0.5; int numberOfSidebands = 10; public double getModAmp() { return modAmp; } public double getModFreq() { return modFreq; } public double getCarrierFreq() { return carrierFreq; } public void setModAmp(double n) { modAmp = n; } public void setModFreq(double n) { modFreq = n; } public void setCarrierAmp(double n) { carrierAmp = n; } public void setCarrierFreq(double n) { carrierFreq = n; } public void setNumberOfSidebands(int n) { numberOfSidebands = n; } void calculateIndexOfModulation() { indexOfModulation = modAmp / modFreq; } boolean odd(int i) { return (i % 2) == 1; } SpectrumDatum calculateDatum(int sidebandNum, int sign) { double besselResult; double freq = carrierFreq + sign * sidebandNum * modFreq; if (sidebandNum == 0) { besselResult = Bessel.bessj0(indexOfModulation); } else if (sidebandNum == 1) { besselResult = Bessel.bessj1(indexOfModulation); } else { besselResult = Bessel.bessj(sidebandNum, indexOfModulation); } double amp; if ((sign == -1) && odd(sidebandNum)) { amp = -1.0 * besselResult; } else { amp = besselResult; } return new SpectrumDatum(freq, amp); } public double getIndexOfModulation() { return indexOfModulation; } /** Calculate Index of Modulation and sidebands, stuff results into a Vector */ public void calculateSpectrum() { calculateIndexOfModulation(); spectrum = new Vector(); for (int i = 1; i <= numberOfSidebands; i++) { spectrum.addElement(calculateDatum(i, 1)); spectrum.addElement(calculateDatum(i, -1)); } spectrum.addElement(calculateDatum(0, 1)); } public Vector getSpectrum() { return spectrum; } public static void main(String args[]) { FMSpectrumCalculator fmsc = new FMSpectrumCalculator(); fmsc.calculateSpectrum(); Vector spec = fmsc.getSpectrum(); JMSL.out.println(spec + ""); } }