【问题标题】:how to make a formula to find the value of D from values in another formula如何制作公式以从另一个公式中的值中找到 D 的值
【发布时间】:2019-12-31 04:36:52
【问题描述】:

在我的 Pulse Wave 发生器中,我需要从 cycleFrequency (f)、cycleRange (r)、minDutyCycle (m) 和 dutyCycle d 中找到 cyclePoint (c) 的值。

这是我创建的一个公式,它从另一个值中找到 dutyCycle (d) 的值 D = ((c/(f/2))r)+m

我不是最擅长代数,所以我可能用错了括号。

这是我的代码

public class PulseGenerator extends SquareGenerator {

    // constants
    public static final double DEF_MIN_DUTY_CYCLE = 0.05;
    public static final double DEF_MAX_DUTY_CYCLE = 0.95;
    public static final double DEF_CYCLE_FREQ = 2;
    public static final double DEF_HOLD_CYCLE = 0;

    // instance variables
    double minDutyCycle;
    double maxDutyCycle;
    double cycleFreq;
    double holdCycle;
    double dutyCycleRange;
    boolean setDirection;

    // constructor
    public PulseGenerator(double amplitude, double frequency, int bitRate,
            double duration, double dutyCycle, double minDutyCycle,
            double maxDutyCycle, double cycleFreq, double holdCycle) {
        super(amplitude, frequency, bitRate, duration, dutyCycle);
        // sample data
        squareSample = new int[sampleLength];
        calculateAmpLimit();
        this.dutyCycle = dutyCycle;
        waveLength = sampleRate / this.frequency;
        this.minDutyCycle = minDutyCycle;
        this.maxDutyCycle = maxDutyCycle;
        this.cycleFreq = cycleFreq * sampleRate;
        this.holdCycle = holdCycle * sampleRate;
        dutyCycleRange = this.maxDutyCycle - this.minDutyCycle;
        setDirection = false;
    }

    // one arg cunstructor
    public PulseGenerator(double frequency) {
        this(AMPLITUDE, frequency, BIT_RATE, DURATION, DEF_DUTY_CYCLE,
                DEF_MIN_DUTY_CYCLE, DEF_MAX_DUTY_CYCLE, DEF_CYCLE_FREQ,
                DEF_HOLD_CYCLE);
    }

    // no args constructor
    public PulseGenerator() {
        this(AMPLITUDE, FREQUENCY, BIT_RATE, DURATION, DEF_DUTY_CYCLE,
                DEF_MIN_DUTY_CYCLE, DEF_MAX_DUTY_CYCLE, DEF_CYCLE_FREQ,
                DEF_HOLD_CYCLE);
    }

    // generate waveform method
    @Override
    public int[] generateWaveForm() {

        // define the decimal j
        double j = 1;

        // define cycle point
        // here is where I need to find the value of cycle point
        int cyclePoint = (int)((dutyCycle * (cycleFreq / 2) - minDutyCycle) / dutyCycleRange);

        System.out.println("Cycle point: " + cyclePoint);

        // generate the actual waveform
        for (int i = 0; i < sampleLength; i++, j++) {

            double waveCycleRatio = waveLength * dutyCycle;

            // same as square
            // draws the wave
            if (j - waveCycleRatio < 0.0) {
                finePoint = 1.0;
            } else if (j - waveCycleRatio >= 0.0 
                    && j - waveCycleRatio < 1) {
                finePoint = 0 - (j - waveCycleRatio - 0.5) * 2;
            } else if (j - waveLength < 0.0) {
                finePoint = -1.0;
            } else if (j - waveLength >= 0.0) {
                finePoint = (j - waveLength - 0.5) * 2;
            }

            // checks if j is equal to wavelength
            if (j == waveLength) {
                j = 1;
            } else if (j - waveLength > 0.0 && j - waveLength < 1.0) {
                j = (j - waveLength);
            }
            point = (int)(finePoint * ampLimit);
            squareSample[i] = point;

            if (holdCycle > 0) {
                holdCycle--;
            } else {
                // implementation of formula to find duty cycle
                dutyCycle = (cyclePoint / (cycleFreq / 2) * dutyCycleRange)
                        + minDutyCycle;
                if (cyclePoint < cycleFreq / 2 && !setDirection) {
                    cyclePoint++;
                } else if (cyclePoint >= cycleFreq / 2 && !setDirection) {
                    cyclePoint--;
                    setDirection = true;
                } else if (cyclePoint > 0 && setDirection) {
                    cyclePoint--;
                } else if (cyclePoint <= 0 && setDirection) {
                    cyclePoint++;
                    setDirection = false;
                }
            }
        }

        // return the sample data
        return squareSample;
    }

}

【问题讨论】:

  • 好的,将d = ((C / (F/2)) * R) + m 重新排列为C: d - m = (C / (F/2)) * R; (d - m) / R = C / (F/2); (F/2) * (d - m) / R = C

标签: java algebra waveform


【解决方案1】:

我认为这条线有点偏离:

int cyclePoint = (int)((dutyCycle * (cycleFreq / 2) - minDutyCycle) / dutyCycleRange);

应该是这样的:

int cyclePoint = (int)((cycleFreq / 2) * (dutyCycle - minDutyCycle) / dutyCycleRange);

【讨论】:

  • 谢谢你,它解决了这个问题。你真的帮了我。但是我该怎么做呢?
  • 查看我在问题部分的评论,开头是“Ok so rearranging”。
【解决方案2】:

此代码行:

int cyclePoint = (int)((dutyCycle * (cycleFreq / 2) - minDutyCycle) / dutyCycleRange);

应替换为:

int cyclePoint = (int) (((dutyCycle - minDutyCycle) * cycleFreq) / (2 * dutyCycleRange));

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多