olgamiller / SSTVEncoder2

Send images via SSTV
Apache License 2.0
113 stars 15 forks source link

Time Accuracy: frequency changed on boundary will cause sample value shift #13

Open 6769 opened 1 year ago

6769 commented 1 year ago

Problem

Every time frequency changed, the remain part of last frequeny lost_Δt ≤ frequency_duration%(1/sample_frequency) will be dropped, in example graph which is between last frequency black sample dot (t<2) and frequeny change point (t=2).

Code Time Function: Brown Real Mathematica Function: Blue sinNx2

I think the real next sample value should be sampled at the red star, and the lost part should be very minor so that it's usually hard to distinguish the effect.

Cause

The time lost happens on these codeMode.java, especially the force (int) cause lost

protected int convertMsToSamples(double durationMs) {
        return (int) Math.round(durationMs * mSampleRate / 1000.0);
    }

    protected void setTone(double frequency) {
        mRunningIntegral += 2.0 * frequency * Math.PI / mSampleRate;
        mRunningIntegral %= 2.0 * Math.PI;
        mOutput.write(Math.sin(mRunningIntegral));
    }
olgamiller commented 1 year ago

Thank you for pointing it out. We already suspected this during implementation, but we went ahead because there wasn't a major problem. It would be nice if you could improve it and create a pull request.