# KOZARD Cleanup/Fixing of NTF Synthesis - Demo #1
# August 8, 2022
#
# Cleanup/Fixing Needed Due To:
#
# 1. deltasigma is broken due to "AttributeError: module 'fractions' has no attribute 'gcd'"
# 2. Beginners might now know how to install a fork as opposed to the original (now unsupported and broken) package
# 3. The automated testing (NOSE) appears to also be broken even with the fixed/forked package
# 4. Many users want to use the examples and it might not be clear to beginners how to go from the Jupyter notebooks to working examples on their installation
#
#
#
# Philosophy of this Cleanedup/Fixed Demo:
#
# 1. Make no use of any additional complications for the beginners (NOSE, Jupyter, etc.)
# 2. Note all installation steps including fixing the broken package
#
#
#
# Getting Started:
#
# 1. Install Latest Python 3:
# - Goto
https://www.python.org/downloads/
# - For Windows I Installed “python-3.10.5-amd64.exe”
# - Setup/Install Notes: Do Select "Add Python to Path"
# - Setup/Install Notes: Do Select "Install for All Users"
#
# 2. Install GIT so that you can later install the fixed fork of the deltasigma package
# - For Windows I went to:
https://git-scm.com/download/win
# - Check installation from CMD with "git version" which replied "git version 2.37.1.windows.1" for my installation
#
# 3. Install the forked and fixed deltasigma package:
# - From CMD (run as admin) I used "pip install git+
https://github.com/Y-F-Acoustics/python-deltasigma.git"
# - From the above you can see the location of the fork I used: "
https://github.com/Y-F-Acoustics/python-deltasigma.git"
#
# 4. Run this module in IDLE (which should install in step 1.)
#
#
#
# After all of that the following "should" work fine assuming nothing else breaks between now (August 8, 2022) and when you try it
#
#
#imports
from
future import division
from deltasigma import *
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
#
#
#start of example
order = 5
OSR = 32
#
#
# Synthesize
H0 = synthesizeNTF(order, OSR, opt=0)
#
# Plot the singularities
plt.subplot(121)
plotPZ(H0, markersize=5)
plt.title('NTF Poles and Zeros')
f = np.concatenate((np.linspace(0, 0.75/OSR, 100), np.linspace(0.75/OSR, 0.5, 100)))
z = np.exp(2j*np.pi*f)
magH0 = dbv(evalTF(H0, z))
#
#
# Plot the magnitude responses
plt.subplot(222)
plt.plot(f, magH0)
figureMagic([0, 0.5], 0.05, None, [-100, 10], 10, None, (16, 8))
plt.xlabel('Normalized frequency ($1\\rightarrow f_s)$')
plt.ylabel('dB')
plt.title('NTF Magnitude Response')
#
#
# Plot the magnitude responses in the signal band
plt.subplot(224)
fstart = 0.01
f = np.linspace(fstart, 1.2, 200)/(2*OSR)
z = np.exp(2j*np.pi*f)
magH0 = dbv(evalTF(H0, z))
plt.semilogx(f*2*OSR, magH0)
plt.axis([fstart, 1.2, -100,- 30])
plt.grid(True)
sigma_H0 = dbv(rmsGain(H0, 0, 0.5/OSR))
#plt.semilogx([fstart, 1], sigma_H0*np.array([1, 1]))
plt.semilogx([fstart, 1], sigma_H0*np.array([1, 1]),'-o')
plt.text(0.15, sigma_H0 + 5, 'rms gain = %5.0fdB' % sigma_H0)
plt.xlabel('Normalized frequency ($1\\rightarrow f_B$)')
plt.ylabel('dB')
plt.tight_layout()
# Always comically missing from beginner demos for some reason
plt.show()