-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsk.py
59 lines (47 loc) · 1.32 KB
/
fsk.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
# 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, generate_audio
# The sampling rate of the analog to digital convert
sampling_rate = 48000.0
# frequency of symbols to generate
frequency_list = [1000.0, 2000.0]
# symbol length in secounds
duration = 0.01 # secounds
# number of symbol types. for mFSK 2 is enough.
num_frequency = 2
# amplitude of the audio
amplitude = 16000
# audio file to be saved into
try:
audiofile = sys.argv[1]
except IndexError:
audiofile = "test.wav"
# generate symbols
symbol = FSK_generate_symbols(frequency_list, duration, sampling_rate)
# data to modulate
data = [0, 1, 1, 0, 0, 1]
import random
data = [random.getrandbits(1) for i in range(100)]
# 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()