## Question 1


```python
import matplotlib.pyplot as plt
import math
import statistics as st
import numpy as np
import scipy.linalg as la
from datetime import datetime
import sympy as sym
```


```python
def myawgn(PSD,B,Fs,l):
    sigma = math.sqrt(2*PSD*B)
    #sigma is the standard deviation
    print("Power = ",2*PSD*B)
    
    N = 1000   
    x = sigma*(np.random.randn(N,l))
    awgn_sequence = x[0]
    
    y = abs(np.fft.fft(x))
    y_conjugate = np.conjugate(y)
    y_squared = np.multiply(y,y_conjugate)
    y_mean = np.mean(y_squared,axis = 1)
    y_mean = y_mean/(Fs*l);
    calculated_psd = np.mean(y_mean);
    
    print("Calculated PSD = ",calculated_psd)
    
    #plotting
    plt.figure()
    plt.plot(y_mean, color = 'green', label='Mean using sample functions')
    plt.axhline(y=calculated_psd, color='r', linestyle='-', label = 'Calculated PSD')
    plt.axhline(y=PSD, color='blue', linestyle='dotted', label = 'Given PSD')
    plt.legend()
    plt.show
    
    return awgn_sequence
```


```python
PSD = float(input('PSD: '))
B = float(input('Bandwidth: '))
Fs = float(input('Sampling frequency: '))
l = int(input('Length of the sequence: '))
awgn = myawgn(PSD,B,Fs,l)
print("AWGN sequence: ",awgn)
```

## Question 2


```python
# library function to check PSD
def posSymDef(mat):
    return np.all(np.linalg.eigvals(mat) > 0)

def mygauss(mu,cov,s):
    
    eigval,m1 = la.eig(cov);    
    m1 = np.array(m1).astype(float);
    m1_inv = np.linalg.inv(m1);
    m2 = np.matmul(m1_inv,cov);
    m2 = np.matmul(m2,m1);
    
    for i in range(len(m2)):
        m2[i][i] = np.sqrt(m2[i][i]);

    M = np.matmul(m1,m2);
    M = np.matmul(M,m1_inv);
    ans = np.matmul(np.random.randn(s, len(m2)),M);
    
    for i in range(s):
        ans[i] = ans[i] + mu;
    print('\nSamples:');
    print(ans);
    
    print('\nCalculated Mean Vector:');
    mu = [];
    ans = np.transpose(ans);
    for i in range(len(m2)): 
        mu.append(np.mean(ans[i]));
    print(mu);
    
    print('\nCalculated Covariance Matrix:');
    print(np.cov(ans));
    print('\nCalculated matrix SPD?: '+str(posSymDef(cov)));
```


```python
cov = np.array([[2, -1, 0],
                [-1, 2, -1],
                [0, -1, 2]]);
mu = np.array([1,3,-5]);

print('Entered Mean Vector:');
print(mu);
print('\nEntered Covariance Matrix:');
print(cov);

mygauss(mu,cov,10000);
```

    Entered Mean Vector:
    [ 1  3 -5]
    
    Entered Covariance Matrix:
    [[ 2 -1  0]
     [-1  2 -1]
     [ 0 -1  2]]
    
    Samples:
    [[ 1.07126392  1.57325674 -1.99812429]
     [ 1.8310989   2.95882931 -5.24555917]
     [ 0.24695964  1.16748971 -4.66019658]
     ...
     [ 1.80900553  6.01039805 -6.02386182]
     [ 3.8905174   0.82114425 -6.22760604]
     [ 1.70671262  5.53382482 -9.03190338]]
    
    Calculated Mean Vector:
    [1.0353198096946992, 2.953741212848046, -4.984730914314471]
    
    Calculated Covariance Matrix:
    [[ 2.0194349  -1.02033005  0.01733812]
     [-1.02033005  2.0414831  -1.02638963]
     [ 0.01733812 -1.02638963  2.01643282]]
    
    Calculated matrix SPD?: True



```python

```

4fa45d199a01cfdb41826fd98a357a9b2732041a
ipynb
Jupyter Notebook
Lab 3/E1.ipynb
shantanutyagi67/CT303_Labs
f1303cd9e8665dccfd1a60b07e3ac2713dff8a47
MIT
2020-10-12T12:03:33.000Z
2020-10-12T12:03:33.000Z
Lab 3/E1.ipynb
shantanutyagi67/CT303_Labs
f1303cd9e8665dccfd1a60b07e3ac2713dff8a47
MIT
Lab 3/E1.ipynb
shantanutyagi67/CT303_Labs
f1303cd9e8665dccfd1a60b07e3ac2713dff8a47
MIT
Qwen/Qwen-72B
1. YES
2. YES