"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.io import wavfile as wav
import pyaudio
import wave
import struct
import soundfile as sf

fs = 44100
CHUNK = 1024
FORMAT = pyaudio.paFloat32
CHANNELS = 1
RATE = 44100
SIZE = 4
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
ofdm_dat =[0.50438429, -0.02383506,  0.58791467,  0.2348634 ,  0.13485915,
       -0.11080575,  0.14255649, -0.17816969,  0.07934755,  0.36257679,
        0.01484431, -0.25610312,  0.06222003,  0.06184105,  0.06806754,
        0.12414788,  0.49612403,  0.23745308, -0.12500415,  0.05437249,
       -0.1667376 , -0.30418769,  0.67402855, -0.23875165,  0.17123811,
       -0.57676341, -0.00702546, -0.42336599, -0.64073441,  0.24434204,
        0.09062861, -0.21336967,  0.48288104,  0.68359124, -0.28106374,
        0.22394743,  0.36567368,  0.39685505, -0.07354058, -0.02984401,
       -0.24900601, -0.01224511, -0.0380252 ,  0.31660546, -0.06931986,
       -0.05525473,  0.01330985, -0.38139859,  0.64287817,  0.35205044,
       -0.17678054,  0.10771653, -0.22297873,  0.22141464, -0.04991556,
       -0.00640337,  0.10272111,  0.0126348 ,  0.21784318, -0.08430732,
        0.30544285,  0.03461279,  0.26233411, -0.0879713 , -0.29783648,
        0.04154521, -0.28042301,  0.05050151,  0.15700041, -0.31364443,
       -0.26617446,  0.19041035,  0.21914105,  0.39092318,  0.03046041,
       -0.17279023,  0.26434422,  0.25970431, -0.53973321,  0.11569725,
        0.12715331,  0.36844235, -0.1567974 , -0.09376671, -0.36435549,
        0.42930377, -0.24807916, -0.5036447 ,  0.03835237, -0.09138526,
       -0.05349345, -0.50167206,  0.03966604, -0.12352192, -0.44959255,
       -0.38403248, -0.50200066,  0.35599741,  0.26445099, -0.16419684,
        0.07820834, -0.50926054, -0.27228183,  0.05997032, -0.25109844,
       -0.15001374, -0.48008597,  0.19983176,  0.29863763, -0.04363729,
        0.01569846,  0.23966606, -0.49605922,  0.4489218 ,  0.18496298,
        0.31201434, -0.45289119,  0.22127547,  0.24891608, -0.00809848,
        0.23583416, -0.31085401,  0.08028209,  0.12650228, -0.08718121,
       -0.51353974, -0.2406631 , -0.29602705, -0.48039771,  0.50438429,
       -0.02383506,  0.58791467,  0.2348634 ,  0.13485915, -0.11080575,
        0.14255649, -0.17816969,  0.07934755,  0.36257679,  0.01484431,
       -0.25610312,  0.06222003,  0.06184105,  0.06806754,  0.12414788,0.12414788,
           0,0,0,0,0,0,0,0,0.50438429,
            0.50438429, -0.02383506,  0.58791467,  0.2348634 ,  0.13485915,
       -0.11080575,  0.14255649, -0.17816969,  0.07934755,  0.36257679,
        0.01484431, -0.25610312,  0.06222003,  0.06184105,  0.06806754,
        0.12414788,  0.49612403,  0.23745308, -0.12500415,  0.05437249,
       -0.1667376 , -0.30418769,  0.67402855, -0.23875165,  0.17123811,
       -0.57676341, -0.00702546, -0.42336599, -0.64073441,  0.24434204,
        0.09062861, -0.21336967,  0.48288104,  0.68359124, -0.28106374,
        0.22394743,  0.36567368,  0.39685505, -0.07354058, -0.02984401,
       -0.24900601, -0.01224511, -0.0380252 ,  0.31660546, -0.06931986,
       -0.05525473,  0.01330985, -0.38139859,  0.64287817,  0.35205044,
       -0.17678054,  0.10771653, -0.22297873,  0.22141464, -0.04991556,
       -0.00640337,  0.10272111,  0.0126348 ,  0.21784318, -0.08430732,
        0.30544285,  0.03461279,  0.26233411, -0.0879713 , -0.29783648,
        0.04154521, -0.28042301,  0.05050151,  0.15700041, -0.31364443,
       -0.26617446,  0.19041035,  0.21914105,  0.39092318,  0.03046041,
       -0.17279023,  0.26434422,  0.25970431, -0.53973321,  0.11569725,
        0.12715331,  0.36844235, -0.1567974 , -0.09376671, -0.36435549,
        0.42930377, -0.24807916, -0.5036447 ,  0.03835237, -0.09138526,
       -0.05349345, -0.50167206,  0.03966604, -0.12352192, -0.44959255,
       -0.38403248, -0.50200066,  0.35599741,  0.26445099, -0.16419684,
        0.07820834, -0.50926054, -0.27228183,  0.05997032, -0.25109844,
       -0.15001374, -0.48008597,  0.19983176,  0.29863763, -0.04363729,
        0.01569846,  0.23966606, -0.49605922,  0.4489218 ,  0.18496298,
        0.31201434, -0.45289119,  0.22127547,  0.24891608, -0.00809848,
        0.23583416, -0.31085401,  0.08028209,  0.12650228, -0.08718121,
       -0.51353974, -0.2406631 , -0.29602705, -0.48039771,  0.50438429,
       -0.02383506,  0.58791467,  0.2348634 ,  0.13485915, -0.11080575,
        0.14255649, -0.17816969,  0.07934755,  0.36257679,  0.01484431,
       -0.25610312,  0.06222003,  0.06184105,  0.06806754,  0.12414788,0.12414788,
           0,0,0,0,0,0,0,0,0,0.50438429,
            0.50438429, -0.02383506,  0.58791467,  0.2348634 ,  0.13485915,
       -0.11080575,  0.14255649, -0.17816969,  0.07934755,  0.36257679,
        0.01484431, -0.25610312,  0.06222003,  0.06184105,  0.06806754,
        0.12414788,  0.49612403,  0.23745308, -0.12500415,  0.05437249,
       -0.1667376 , -0.30418769,  0.67402855, -0.23875165,  0.17123811,
       -0.57676341, -0.00702546, -0.42336599, -0.64073441,  0.24434204,
        0.09062861, -0.21336967,  0.48288104,  0.68359124, -0.28106374,
        0.22394743,  0.36567368,  0.39685505, -0.07354058, -0.02984401,
       -0.24900601, -0.01224511, -0.0380252 ,  0.31660546, -0.06931986,
       -0.05525473,  0.01330985, -0.38139859,  0.64287817,  0.35205044,
       -0.17678054,  0.10771653, -0.22297873,  0.22141464, -0.04991556,
       -0.00640337,  0.10272111,  0.0126348 ,  0.21784318, -0.08430732,
        0.30544285,  0.03461279,  0.26233411, -0.0879713 , -0.29783648,
        0.04154521, -0.28042301,  0.05050151,  0.15700041, -0.31364443,
       -0.26617446,  0.19041035,  0.21914105,  0.39092318,  0.03046041,
       -0.17279023,  0.26434422,  0.25970431, -0.53973321,  0.11569725,
        0.12715331,  0.36844235, -0.1567974 , -0.09376671, -0.36435549,
        0.42930377, -0.24807916, -0.5036447 ,  0.03835237, -0.09138526,
       -0.05349345, -0.50167206,  0.03966604, -0.12352192, -0.44959255,
       -0.38403248, -0.50200066,  0.35599741,  0.26445099, -0.16419684,
        0.07820834, -0.50926054, -0.27228183,  0.05997032, -0.25109844,
       -0.15001374, -0.48008597,  0.19983176,  0.29863763, -0.04363729,
        0.01569846,  0.23966606, -0.49605922,  0.4489218 ,  0.18496298,
        0.31201434, -0.45289119,  0.22127547,  0.24891608, -0.00809848,
        0.23583416, -0.31085401,  0.08028209,  0.12650228, -0.08718121,
       -0.51353974, -0.2406631 , -0.29602705, -0.48039771,  0.50438429,
       -0.02383506,  0.58791467,  0.2348634 ,  0.13485915, -0.11080575,
        0.14255649, -0.17816969,  0.07934755,  0.36257679,  0.01484431,
       -0.25610312,  0.06222003,  0.06184105,  0.06806754,  0.12414788,0.12414788,
           0,0,0,0,0,0,0,0,0,0,0,0,0,0.50438429,
        0.50438429, -0.02383506,  0.58791467,  0.2348634 ,  0.13485915,
       -0.11080575,  0.14255649, -0.17816969,  0.07934755,  0.36257679,
        0.01484431, -0.25610312,  0.06222003,  0.06184105,  0.06806754,
        0.12414788,  0.49612403,  0.23745308, -0.12500415,  0.05437249,
       -0.1667376 , -0.30418769,  0.67402855, -0.23875165,  0.17123811,
       -0.57676341, -0.00702546, -0.42336599, -0.64073441,  0.24434204,
        0.09062861, -0.21336967,  0.48288104,  0.68359124, -0.28106374,
        0.22394743,  0.36567368,  0.39685505, -0.07354058, -0.02984401,
       -0.24900601, -0.01224511, -0.0380252 ,  0.31660546, -0.06931986,
       -0.05525473,  0.01330985, -0.38139859,  0.64287817,  0.35205044,
       -0.17678054,  0.10771653, -0.22297873,  0.22141464, -0.04991556,
       -0.00640337,  0.10272111,  0.0126348 ,  0.21784318, -0.08430732,
        0.30544285,  0.03461279,  0.26233411, -0.0879713 , -0.29783648,
        0.04154521, -0.28042301,  0.05050151,  0.15700041, -0.31364443,
       -0.26617446,  0.19041035,  0.21914105,  0.39092318,  0.03046041,
       -0.17279023,  0.26434422,  0.25970431, -0.53973321,  0.11569725,
        0.12715331,  0.36844235, -0.1567974 , -0.09376671, -0.36435549,
        0.42930377, -0.24807916, -0.5036447 ,  0.03835237, -0.09138526,
       -0.05349345, -0.50167206,  0.03966604, -0.12352192, -0.44959255,
       -0.38403248, -0.50200066,  0.35599741,  0.26445099, -0.16419684,
        0.07820834, -0.50926054, -0.27228183,  0.05997032, -0.25109844,
       -0.15001374, -0.48008597,  0.19983176,  0.29863763, -0.04363729,
        0.01569846,  0.23966606, -0.49605922,  0.4489218 ,  0.18496298,
        0.31201434, -0.45289119,  0.22127547,  0.24891608, -0.00809848,
        0.23583416, -0.31085401,  0.08028209,  0.12650228, -0.08718121,
       -0.51353974, -0.2406631 , -0.29602705, -0.48039771,  0.50438429,
       -0.02383506,  0.58791467,  0.2348634 ,  0.13485915, -0.11080575,
        0.14255649, -0.17816969,  0.07934755,  0.36257679,  0.01484431,
       -0.25610312,  0.06222003,  0.06184105,  0.06806754,  0.12414788]

plt.plot(ofdm_dat)
sf.write("org_float.wav", ofdm_dat, fs)

#Over Sample
y = ofdm_dat
x = np.arange(len(y))
f = interpolate.interp1d(x, y)
f2 = interpolate.interp1d(x, y, kind='cubic')
sampleRate = 1/2
xOverSample = np.arange(0, len(y)-1, sampleRate)
yOverSample = f(xOverSample)   # use interpolation function returned by `interp1d`

sf.write("inter2_float.wav", yOverSample, fs)

#float to int16
int16_dat = np.array(yOverSample)*32767
int16_dat = int16_dat.astype(np.int16)
sf.write("inter2_int16.wav", int16_dat, fs)

record =np.array([ 11303,   7769,  -1274,   6426,  14145,  10974,   5764,   4619,
         3774,    621,  -2509,     63,   3772,     38,  -4540,  -1698,
         1754,   5081,   8940,   5245,    616,  -2703,  -6429,  -3025,
         1480,   1665,   1686,   1597,   1402,   2216,   2739,   7116,
        12162,   9390,   5870,   1808,  -2887,  -1234,   1351,  -1133,
        -3913,  -5563,  -8067,   2682,  16367,   7036,  -5959,  -2012,
         3915,  -4195, -14215,  -8139,     19,  -4913, -10645, -13021,
       -16040,  -6445,   5776,   4675,   2276,  -1182,  -5689,   2113,
        11573,  14448,  17470,   6674,  -6862,  -1724,   5121,   7238,
         9071,   9684,  10157,   4548,  -1926,  -1648,   -450,  -3171,
        -6019,  -3551,   -389,   -677,  -1251,   2778,   7627,   3545,
        -1543,  -1598,  -1360,   -828,    305,  -3898,  -9710,   1672,
        15475,  12698,   8936,   2991,  -4355,  -1670,   2454,   -852,
        -5401,   -832,   5303,   2445,  -1372,   -916,   -334,    743,
         2291,   1592,    247,   2315,   5347,   2073,  -2187,   1952,
         7208,   4560,    643,   3132,   6459,   2461,  -2191,  -4623,
        -7291,  -3920,    702,  -2681,  -7163,  -3805,    701,   2186,
         3961,  -1317,  -7632,  -7316,  -7006,  -1743,   4343,   5088,
         5351,   7300,   9682,   5620,    520,  -1695,  -4452,    443,
         6539,   6483,   6916,  -2256, -13591,  -6556,   2282,   2883,
         2906,   5625,   9242,   3389,  -4147,  -3690,  -2477,  -5487,
        -9391,   -613,  10579,   3173,  -6035,  -9102, -12963,  -7155,
          455,   -620,  -2305,  -2258,  -1675,  -6684, -13050,  -6915,
          492,   -994,  -2931,  -6769, -11049, -10825,  -9812, -11068,
       -12846,  -3244,   8180,   7776,   6218,   1768,  -3969,  -1792,
         1834,  -4438, -12387, -10136,  -7029,  -3439,   1422,  -1778,
        -5990,  -5333,  -3886,  -7406, -12094,  -4531,   4761,   6235,
         7293,   3723,   -971,   -490,    406,   2885,   6383,  -1697,
       -12487,  -2184,  11000,   8569,   4784,   6067,   7964,   -636,
       -11356,  -3996,   5270,   5790,   6321,   3629,    -78,   2102,
         5682,    -19,  -7815,  -3781,   1700,   2395,   3029,    689,
        -2130,  -7107, -12699,  -9887,  -5840,  -6674,  -7034,  -9276,
       -12262,  -1358,  12059,   7062,   -774,   6018,  14444,  10854,
         5717,   4736,   3554,    708,  -2630,     24,   3883,    168,
        -4421,  -1556,   1937,   5082,   8943,   5312,    527,  -2819,
        -6488,  -2950,   1693,   1727,   1671,   1619,   1716,   2438,
         2980,   7340,  12602,   6868,   -732,   5950,  14521,  10902,
         5952,   4757,   3502,    627,  -2604], dtype='int16')

#int16 to float
float_dat = np.array(record)/32767

#sampling
K = len(float_dat)
P = int(K*sampleRate)
allIndexs= np.arange(K) 
sampleIndexs= allIndexs[::K//P] 
ySample = float_dat[sampleIndexs]
xSample = np.arange(len(ySample))

plt.plot(ofdm_dat,'r')
plt.plot(ySample,'g')
plt.show()

plt.plot(x, y, 'bo', xOverSample, yOverSample, '-x', xSample, ySample, 'rd')
plt.legend(['data', 'linear'], loc='best')
plt.show()


