generate the i (in-phase) and q (quadrature) components of a signal with fc = 1.0925e9; fs = 100.0e6; in javascript

To generate the i (in-phase) and q (quadrature) components of a signal in JavaScript, you can use the following code:

index.tsx
function generateSignal(fc, fs) {
  const numSamples = 1000; // Number of samples
  const amplitude = 1; // Amplitude of the signal
  const i = []; // In-phase component array
  const q = []; // Quadrature component array

  const omega = 2 * Math.PI * fc / fs; // Angular frequency

  for(let n = 0; n < numSamples; n++) {
    // Generate the in-phase and quadrature components
    const inPhase = amplitude * Math.cos(omega * n);
    const quadrature = amplitude * Math.sin(omega * n);

    i.push(inPhase);
    q.push(quadrature);
  }

  return { i, q };
}

// Example usage
const fc = 1.0925e9; // Carrier frequency
const fs = 100.0e6; // Sampling frequency

const signal = generateSignal(fc, fs);
console.log(signal.i); // In-phase component
console.log(signal.q); // Quadrature component
794 chars
28 lines

In this code, the generateSignal function takes the carrier frequency fc and sampling frequency fs as parameters. It uses the formula inPhase = amplitude * cos(omega * n) and quadrature = amplitude * sin(omega * n) to calculate the in-phase and quadrature components at each sample point n. The function returns an object containing two arrays i and q, which represent the in-phase and quadrature components of the signal respectively.

You can adjust the numSamples variable to control the number of samples generated and adjust the amplitude variable to control the amplitude of the signal.

Note that the code uses the Math object in JavaScript to perform the necessary trigonometric calculations.

Make sure to provide valid values for the carrier frequency (fc) and sampling frequency (fs) to get the correct components.

related categories

gistlibby LogSnag