import './seismogram.js';

/*
 * Philip Crotwell
 * University of South Carolina, 2019
 * http://www.seis.sc.edu
 */
function taper(seis, width = 0.05, taperType = HANNING) {
  if (width > 0.5) {
    throw new Error("Taper width cannot be larger than 0.5, width=" + width);
  }

  if (seis.isContiguous()) {
    let data = seis.y;
    let outData = Float32Array.from(data);
    let w = Math.floor(data.length * width);
    let coeff = getCoefficients(taperType, w);
    const omega = coeff[0];
    const f0 = coeff[1];
    const f1 = coeff[2];

    for (let i = 0; i < w; i++) {
      const taperFactor = f0 - f1 * Math.cos(omega * i);
      outData[i] = outData[i] * taperFactor;
      outData[outData.length - i - 1] = outData[outData.length - i - 1] * taperFactor;
    }

    return seis.cloneWithNewData(outData);
  } else {
    throw new Error("Cannot take taper of non-contiguous seismogram");
  }
}
/**
 * Calculates the coefficients for tapering, [omega, f0, f1]
 */

function getCoefficients(type, length) {
  let omega, f0, f1;

  if (type === HANNING) {
    omega = Math.PI / length;
    f0 = .5;
    f1 = .5;
  } else if (type === HAMMING) {
    omega = Math.PI / length;
    f0 = .54;
    f1 = .46;
  } else {
    // cosine
    omega = Math.PI / 2 / length;
    f0 = 1;
    f1 = 1;
  }

  return [omega, f0, f1];
}
const HANNING = "HANNING";
const HAMMING = "HAMMING";
const COSINE = "COSINE";

export { COSINE, HAMMING, HANNING, getCoefficients, taper };
//# sourceMappingURL=taper.js.map
