-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__diffuse_freq__.py
More file actions
208 lines (150 loc) · 5.27 KB
/
__diffuse_freq__.py
File metadata and controls
208 lines (150 loc) · 5.27 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import numpy
import scipy.fft
import matplotlib.pyplot as plt
from scipy.io import wavfile
def load_data(filename):
sample_rate, audio_data = wavfile.read(filename)
duration = len(audio_data)/sample_rate
data = [duration, sample_rate, audio_data]
return data
def encrypt_shuffle(input_file, output_file, key_file):
# Get data
data = load_data(input_file)
duration = data[0]
sample_rate = data[1]
audio_data = data[2]
N = int(duration * sample_rate)
# Split audio data into N sub-bands
remaining = len(audio_data) % N
if remaining != 0:
print("Padding on encrypted spectrum")
padding = N - remaining
audio_data = numpy.pad(audio_data, (0, padding), 'constant')
sub_band = numpy.array_split(audio_data, N)
# Shuffle sub-bands
sub_band_idx = numpy.arange(N)
numpy.random.shuffle(sub_band_idx)
sub_band_shuffled = [0] * N
for i in range(N):
sub_band_shuffled[sub_band_idx[i]] = sub_band[i]
# Concatenate sub-bands
enc_audio_data = numpy.concatenate(sub_band_shuffled)
# Remove padding
enc_audio_data = enc_audio_data[:len(audio_data)]
enc_audio_data = enc_audio_data.astype(numpy.int16)
# Write encrypted file
wavfile.write(output_file, sample_rate, enc_audio_data)
# Write key file
key = sub_band_idx
key.tofile(key_file)
def decrypt_shuffle(input_file, output_file, key_file):
# Get data
data = load_data(input_file)
duration = data[0]
sample_rate = data[1]
audio_data = data[2]
N = int(duration * sample_rate)
# Load key
key = numpy.fromfile(key_file, dtype=int)
# Split audio data into N sub-bands
remaining = len(audio_data) % N
if remaining != 0:
print("Padding on decrypted spectrum")
padding = N - remaining
audio_data = audio_data[:-padding]
sub_band = numpy.array_split(audio_data, N)
# Arrange sub-bands
arranged_sub_band = [0] * N
for i in range(N):
arranged_sub_band[i] = sub_band[key[i]]
# Concatenate sub-bands
dec_audio_data = numpy.concatenate(arranged_sub_band)
# Remove padding
dec_audio_data = dec_audio_data[:len(audio_data)]
dec_audio_data = dec_audio_data.astype(numpy.int16)
wavfile.write(output_file, sample_rate, dec_audio_data)
def encrypt(input_file, output_file, key_file):
# Get data
data = load_data(input_file)
duration = data[0]
sample_rate = data[1]
audio_data = data[2]
audio_data = audio_data.astype(numpy.float64)
# Number of sub-bands
N = 4
spectrum = numpy.fft.fft(audio_data)
# Split spectrum into N sub-bands
original_len = len(spectrum)
remaining = len(spectrum) % N
if remaining != 0:
print("Padding on encrypted spectrum")
padding = N - remaining
spectrum = numpy.pad(spectrum, (0, padding), 'constant')
sub_band = numpy.array_split(spectrum, N)
# Shuffle sub-bands
sub_band_idx = numpy.arange(N)
numpy.random.shuffle(sub_band_idx)
sub_band_shuffled = [0] * N
for i in range(N):
sub_band_shuffled[sub_band_idx[i]] = sub_band[i]
spectrum_shuffled = numpy.concatenate(sub_band_shuffled)
encrypted_spectrum = numpy.fft.ifft(spectrum_shuffled)
enc_audio_data = numpy.real(encrypted_spectrum)
# Remove padding
enc_audio_data = enc_audio_data[:original_len]
enc_audio_data = enc_audio_data.astype(numpy.int16)
wavfile.write(output_file, sample_rate, enc_audio_data)
# Plot audio data
# plt.plot(audio_data)
# plt.plot(enc_audio_data)
# plt.show()
# Write key file
key = sub_band_idx
key.tofile(key_file)
def decrypt(input_file, output_file, key_file):
# Get data
data = load_data(input_file)
duration = data[0]
sample_rate = data[1]
audio_data = data[2]
audio_data = audio_data.astype(numpy.float64)
# Number of sub-bands
N = 4
# Load key
key = numpy.fromfile(key_file, dtype=int)
spectrum = numpy.fft.fft(audio_data)
# # Split spectrum into N sub-bands
remaining = len(spectrum) % N
if remaining != 0:
print("Padding on decrypted spectrum")
padding = N - remaining
spectrum = spectrum[:-padding]
sub_band = numpy.array_split(spectrum, N)
arranged_sub_band = [0] * N
for i in range(N):
arranged_sub_band[i] = sub_band[key[i]]
spectrum_arranged = numpy.concatenate(arranged_sub_band)
decrypted_spectrum = numpy.fft.ifft(spectrum_arranged)
dec_audio_data = numpy.real(decrypted_spectrum)
# Remove padding
dec_audio_data = dec_audio_data[:len(audio_data)]
dec_audio_data = dec_audio_data.astype(numpy.int16)
# Plot audio data
# plt.plot(dec_audio_data)
# plt.show()
wavfile.write(output_file, sample_rate, dec_audio_data)
def nyquist_sampling(input_file):
# Get data
data = load_data(input_file)
sample_rate = data[1]
signal = data[2]
nyquist_rate = sample_rate / 2
signal = signal.astype(numpy.float64)
signal = numpy.fft.fftfreq(len(signal), 1/sample_rate)
max_freq = numpy.max(numpy.abs(signal))
if max_freq > nyquist_rate:
print("Signal is not sampled at nyquist rate")
return False
else:
print("Signal is sampled at nyquist rate")
return True