-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsk-256.py
86 lines (70 loc) · 1.89 KB
/
fsk-256.py
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
84
85
86
# used for editing wave files
import wave
# for basic mathematic operations
import numpy as np
# for using with wave functions
import struct
# used for getting arguments
import sys
# for generating symbols
from functions import FSK_generate_symbols_2, generate_audio
# The sampling rate of the analog to digital convert
sampling_rate = 48000.0
# frequency of symbols to generate
frequency_list = [1500.0 + 45.0 * x for x in range(256)]
# symbol length in secounds
"""
wave envelope shape for each sign is like this:
a ________
m / \
p___/ \___
\time<a >< b ><c ><d >
a: rise time
b: time in maximum amplitude
c: fall time
d: time in void
"""
# rise time
rise_duration = 0.010
# time in maximum amplitude
max_duration = 0.050
# fall time
fall_duration = 0.010
# time in void
void_duration = 0.010
# total time
total_duration = rise_duration + max_duration + fall_duration + void_duration
# number of symbol types. for fsk-256 it's 256.
num_frequency = 256
# amplitude of the audio
amplitude = 32000
# audio file to be saved into
try:
audiofile = sys.argv[1]
except IndexError:
audiofile = "test.wav"
# generate symbols
symbol = FSK_generate_symbols_2(frequency_list, (rise_duration, max_duration, fall_duration, void_duration), sampling_rate)
# data to modulate
data = [0, 1, 1, 0, 0, 1]
import random
random.seed(0)
data = [random.randint(0, 256) for i in range(100)]
print(data)
# data = [x for x in range(256)]
# generate an audio based on data fsk
audio = generate_audio(data, symbol)
# file properties
nframes = len(audio)
comptype="NONE"
compname="not compressed"
nchannels=1
sampwidth=2
# open wave file
wav_file=wave.open(audiofile, 'w')
# set properties
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))
# write the audio to file
for s in audio:
wav_file.writeframes(struct.pack('h', int(s*amplitude)))
wav_file.close()