Frequency Shift

[1]:
%pylab inline
pylab.rcParams['savefig.dpi'] = 300
from caf_verilog.freq_shift import FreqShift
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib
[2]:
fs = 625e3
freq_res = 200
n_bits = 8
[3]:
n = np.arange(0,10e3)
x = np.exp(2*np.pi*((50e3)/fs)*n*1j)
[4]:
Px, ff = psd(x,2**10, fs)
plot(ff, 10*np.log10(Px))
xlim([0, 100e3])
ylim([-80, 0])
[4]:
(-80.0, 0.0)
../_images/nb_examples_FrequencyShift_4_1.png
[5]:
fq = FreqShift(x, freq_res, fs, n_bits)
[6]:
fq.gen_tb(20e3)
[7]:
max(fq.x_quant.real), min(fq.x_quant.real), max(fq.x_quant.imag), min(fq.x_quant.imag)
[7]:
(2047.0, -2031.0, 2043.0, -2043.0)
[8]:
max(x.real), min(x.real), max(x.imag), min(x.imag)
[8]:
(1.0, -0.9921147013145629, 0.9980267284283143, -0.9980267284282867)
[9]:
Px_in, ff_in = psd(fq.x_quant,2**10, fs)
plot(ff_in, 10*np.log10(Px_in))
xlim([0, 100e3])
ylim([-80, 40])
savefig('freq_shift_in.png')
../_images/nb_examples_FrequencyShift_9_0.png

Generated Shifted Signal

[10]:
from caf_verilog.io_helper import read_complex_output
gss = read_complex_output(fq.test_output_filename)
sss = read_complex_output(fq.submodules['sig_gen'].test_output_filename)
[11]:
print(len(gss), len(x))
10000 10000
[12]:
assert len(gss) == len(x)
[13]:
print(gss[:5])
[(1015+0j), (779+656j), (174+1006j), (-523+873j), (-962+333j)]
[14]:
Px, ff = psd(sss,2**12, fs)
plot(ff, 10*np.log10(Px))
xlim([15000, 25000])
ylim([-80, 20])
[14]:
(-80.0, 20.0)
../_images/nb_examples_FrequencyShift_15_1.png
[15]:
Px_out, ff_out = psd(gss,2**12, fs)
plot(ff_out, 10*np.log10(Px_out))
xlim([0, 100e3])
savefig('freq_shift_out.png')
../_images/nb_examples_FrequencyShift_16_0.png
[16]:
f, axarr = plt.subplots(2, sharex=True, gridspec_kw={'hspace': 0})
axarr[0].plot(ff_in, 10*np.log10(Px_in))
axarr[0].set_xlim([0, 100e3])
axarr[0].set_ylim([-80, 40])
axarr[1].plot(ff_out, 10*np.log10(Px_out))
axarr[1].set_xlim([0, 100e3])
axarr[1].set_xlabel('Frequency (Hz)')
axarr[0].set_title('Power Spectral Density (dB/Hz)')
f.savefig('freq_shift_inout.png')
../_images/nb_examples_FrequencyShift_17_0.png

Generated Negative Shifted Signal

[17]:
%mkdir -p ./neg_shift
[18]:
fqn = FreqShift(x, freq_res, fs, 8, neg_shift=True, output_dir='./neg_shift')
[19]:
fqn.gen_tb(20000)
[20]:
import os
gssn = read_complex_output(os.path.join(fqn.output_dir, fqn.test_output_filename))
[21]:
assert len(gssn) == len(x)
[22]:
sssn = []
for ss in sss:
    sssn.append(ss.real + (ss.imag*-1j))
[23]:
for ii in range(len(gssn)):
    check_res = fqn.x_quant[ii] * sssn[ii]
    check_i = int(check_res.real) >> 8
    assert gssn[ii].real == check_i
    check_q = int(check_res.imag) >> 8
    assert gssn[ii].imag == check_q
[24]:
Px, ff = psd(gssn,2**12, fs)
plot(ff, 10*np.log10(Px))
xlim([0, 100e3])
ylim([-80, 40])
[24]:
(-80.0, 40.0)
../_images/nb_examples_FrequencyShift_26_1.png