This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsk.h
83 lines (71 loc) · 2.16 KB
/
fsk.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef FSK_H
#define FSK_H
#include "receiversender.h"
namespace fskube {
struct FskParams {
unsigned int samplesPerSecond;
unsigned int bitsPerSecond;
unsigned int markFrequency;
unsigned int spaceFrequency;
// Note that this will probably involve truncation, which means
// that we'll slowly drift. This is fine, as real hardware will
// drift as well, and we need to be able to deal with that.
unsigned int samplesPerBit() {
return samplesPerSecond / bitsPerSecond;
}
double secondsPerBit() {
return 1.0 / bitsPerSecond;
}
double samplesToTime(unsigned int samples) {
return (double) samples / samplesPerSecond;
}
double samplesToTime(double samples) {
return samples / samplesPerSecond;
}
bool frequencyToBit(unsigned int frequency) {
return frequency == markFrequency ? 1 : 0;
}
};
class Modulator : public Sender<bool, double> {
private:
FskParams fsk;
double continuousPhaseOffset;
public:
Modulator(FskParams fsk);
virtual void receive(bool bit);
virtual void reset();
};
struct Sample {
unsigned long long index;
float remainder;
double value;
bool valid;
};
// Histeresis to avoid issues if the signal wavers around zero.
// See nexus5helloworld for an example of some noise around zero at the
// start of transmission.
#define FSK_HIGH_THRESHOLD 0.4
#define FSK_LOW_THRESHOLD -0.4
class Demodulator : public Sender<double, bool> {
private:
FskParams fsk;
unsigned long long sampleIndex;
Sample lastZeroCrossing;
unsigned int insignificantSampleCount;
Sample lastSignificantSample;
unsigned int lastFrequencyHalfSeen;
unsigned int lastFrequencyHalfSeenCount;
void reset();
void addZeroCrossing(Sample sample);
void addFrequencyHalfSeen(unsigned int frequency);
int nearMarks;
int currentMarkStreak;
public:
Demodulator();
Demodulator(FskParams fsk);
void setFskParams(FskParams fsk);
virtual void receive(double value);
void flush();
};
} // namespace fskube
#endif // FSK_H