import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
def F(w):
    if (w>-0.5*np.pi)&(w<0.5*np.pi):
        return np.cos(w)
    else:
        return 0
def f(t):
    return (Sa(0.5*np.pi*(t+1))+Sa(0.5*np.pi*(t-1)))/4

def p(t,T):
    num = int(45/T)
    flag = 0
    for i in range(num):
        a = i-20/T
        if((t>=(a*T-0.01/2))&(t<=(a*T+0.01/2))):
            flag = 1
    if(flag==1) :
        return 1
    else:
        return 0

def fs(t):
    T= 5
    if((t>=(T-0.01/2))&(t<=(0.01/2+T))):
        return f(t)
    else :
        return 0

def Sa(t):
    if (t==0):
        return 1
    else :
        return np.sin(t)/t  

def Fs(w,f):
    num = 0 
    for i in range(2000):

        a = i - 1000
        num += 0.01*f*Sa(a*2*np.pi*f*0.01/2)*F(w-a*2*np.pi*f)
    return num

def H(w,T):
    if (w>-0.5*3.14)&(w<0.5*3.14):
        return T/0.01
    else:
        return 0
	
one_degree = 3.14/180
#w = np.arange(-0.5*3.14,0.5*3.16,0.01)
value = []
w = []
v = 0
for i in range(414):
    v = F(-0.5*3.14-0.5+i*0.01)
    value.append(v)
    w.append(-0.5*3.14+i*0.01)

plt.plot(w,value)
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_1",dpi = 300)

value = []
key = []
a = 0
for i in range(401):
    t = i*0.1-20
    a = f(t)
    key.append(t)
    value.append(a)

plt.plot(key,value)
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_2",dpi = 300)

a = 0
value = []
key = []
T = 5
fs = 1/T
for i in range(40001):
    t = 0.001*i-20
    a = p(t,T)*f(t)
    key.append(t)
    value.append(a)

plt.plot(key,value)
plt.title("f = %.1fHz"%fs)
num = np.arange(-20,21,T)
b = int(40/T)
for i in range(b):
    plt.scatter(num[i],p(num[i],T)*f(num[i]),alpha=0.5)

plt.xlabel('t')
plt.ylabel('fs(t)')
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_9",dpi = 300)

a = 0
value = []
key = []
T = 2
fs = 1/T
for i in range(40001):
    t = 0.001*i-20
    a = p(t,T)*f(t)
    key.append(t)
    value.append(a)

plt.plot(key,value)
num = np.arange(-20,21,T)
b = int(40/T)
for i in range(b):
    plt.scatter(num[i],p(num[i],T)*f(num[i]),alpha=0.5)
plt.title("f = %.1fHz"%fs)
plt.xlabel('t')
plt.ylabel('fs(t)')
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_10",dpi = 300)

a = 0
value = []
key = []
T = 1
fs = 1/T
for i in range(40001):
    t = 0.001*i-20
    a = p(t,T)*f(t)
    key.append(t)
    value.append(a)

plt.plot(key,value)
num = np.arange(-20,21,T)
b = int(40/T)
for i in range(b):
    plt.scatter(num[i],p(num[i],T)*f(num[i]),alpha=0.5)
plt.title("f = %.1fHz"%fs)
plt.xlabel('t')
plt.ylabel('fs(t)')
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_11",dpi = 300)

value = []
key = []
a = 0
f = [0.2,0.5,1]
b = [0,1,2]

for i in range(2001):
    w = i*0.01-10
    a = Fs(w,0.2)
    key.append(w)
    value.append(a)
plt.plot(key,value)
plt.title("f = 0.2Hz")
plt.xlabel('w')
plt.ylabel('Fs(w)')
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_3",dpi = 300)

value = []
key = []
a = 0
f = [0.2,0.5,1]  
for i in range(2001):
    w = i*0.01-10
    a = Fs(w,0.5)
    key.append(w)
    value.append(a)
plt.plot(key,value)
plt.title("f = 0.5Hz")
plt.xlabel('w')
plt.ylabel('Fs(w)')
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_4",dpi = 300)

value = []
key = []
a = 0  
for i in range(2001):
    w = i*0.01-10
    a = Fs(w,1)
    key.append(w)
    value.append(a)
plt.plot(key,value)
plt.title("f = 1Hz")
plt.xlabel('w')
plt.ylabel('Fs(w)')
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_5",dpi = 300)

0.2Hz
value = []
key = []
a = 0
f = 0.2
T = f/1
for i in range(401):
    w = 0.01*i-2
    a = H(w,T)*Fs(w,f)
    key.append(w)
    value.append(a)
plt.plot(key,value)
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("f = {0}Hz时的恢复信号".format(f))
plt.xlabel('w')
plt.ylabel('F\'(w)')
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_6",dpi = 300)

value = []
key = []
a = 0
f = 0.5
T = f/1
for i in range(401):
    w = 0.01*i-2
    a = H(w,T)*Fs(w,f)
    key.append(w)
    value.append(a)
plt.plot(key,value)
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("f = {0}Hz时的恢复信号".format(f))
plt.xlabel('w')
plt.ylabel('F\'(w)')
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_7",dpi = 300)

value = []
key = []
a = 0
f = 1
T = f/1
for i in range(401):
    w = 0.01*i-2
    a = H(w,T)*Fs(w,f)
    key.append(w)
    value.append(a)
plt.plot(key,value)
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("f = {0}Hz时的恢复信号".format(f))
plt.xlabel('w')
plt.ylabel('F\'(w)')
plt.grid()
plt.savefig(r"E:\2021上学期\信号与系统\第二次实验\fig_8",dpi = 300)