I have a 30-seconds wav audio file with a sample rate of 44100Hz, obviously this array of samples is a 1D array, and so when I modulate it in AM (Amplitude Modulation) I get back a 1D array, but I want it to return a 2D array of complex values, I don't really know how can you generate the real and imaginary part from one value and so I ended up here.
Here is my code:
SAMPLE_FOR = 30 # in seconds
samplerate, data = scipy.io.wavfile.read(r'Recording.wav')
data = data[0:int(samplerate*SAMPLE_FOR)]
fm = generateSignalFM(data,time)
def generateSignalAM(slc,t):
samples,time_vec = SamplerateConversion(slc)
w = low_cut_filter(samples,176400,22050)
TWO_PI = 2 * np.pi
carrier_hz = 20000
ac = 0.5
carrier = np.sin(TWO_PI * carrier_hz * time_vec)
envelope = (1.0 + ac * w)
modulated = envelope * carrier
return modulated
And FM signal code:
def generateSignalFM(samples,t, fc=None, b=.3):
w = low_cut_filter(samples,fs,22050)
N = len(w)
if fc is None:
fc = 75000
x0 = w[:N]
# ensure it's [-.5, .5] so diff(phi) is b*[-pi, pi]
x0 /= (2*np.abs(x0).max())
# generate phase
phi0 = 2*np.pi * fc * time_vec
phi1 = 2*np.pi * b * np.cumsum(x0)
phi = phi0 + phi1
diffmax = np.abs(np.diff(phi)).max()
# `b` correction
if diffmax >= np.pi:
diffmax0 = np.abs(np.diff(phi0)).max()
diffmax1 = np.abs(np.diff(phi1)).max()
phi1 *= ((np.pi - diffmax0) / diffmax1)
phi = phi0 + phi1
# modulate
x = np.cos(phi)
return x