0
$\begingroup$

I have multiplexed 32 signals into 1 signal with python. Now I want to plot that signal in time domain and to plot it's amplitude spectrum. Professor gave us his plots so we can look up to it. These are his plots: enter image description here

enter image description here


These are my plots:

enter image description here

enter image description here

As you can see plots in time domain are very similar. They can't be identical because we are using functions for generating random numbers. However if you look on amplitude spectrum they are not so similar. Only similar thing is that they kinda burst away around 30 kHz.

I'm wondering if there is possibility that I didn't make mistake and that I just need to scale somehow y-axis. Can anyone help me or give me more informations.

This is my python block:


import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random


def generate_signal(t, fm):
    Nsf = 0
    while Nsf < 1:
        Nsf = round(random() * 32)
    
    x = np.zeros(len(t))
    
    for _ in range(Nsf):
        znak = 1
        if random() < 0.5:
            znak = -1
        
        r_fm = round(random() * fm)
        r_theta = random() * np.pi
        r_A = random()
        
        x = x + znak * r_A * np.cos(2 * np.pi * r_fm * t + r_theta)
    
    return x


# Parameters
fm = 500  # Maximum frequency in the signal spectrum
Bg = 400  # Bandwidth for frequency multiplexing

# Time vector
delta_t = 0.1 * 1e-6
t_max = 10 * 1e-3
num_samples = int(t_max / delta_t)
t = np.linspace(0, t_max, num_samples)

# Frequency Multiplexing
x_t = np.zeros_like(t)

for k in range(32):
    carrier_frequency = (fm + Bg) * k
    x_t += generate_signal(t, fm) * np.cos(2 * np.pi * carrier_frequency * t)

# Plot the time-domain signal
plt.figure(figsize=(10, 4))
plt.plot(t * 1e3, x_t)
plt.title('Multiplexed Signal in the Time Domain')
plt.xlabel('t (ms)')
plt.ylabel('x (t)')
plt.ylim(-20, 20)
plt.grid(True)
plt.show()

# Compute the Fourier Transform
x_f = np.fft.fft(x_t)

# Compute corresponding frequency values
x_f_freq = np.fft.fftfreq(num_samples, delta_t)
temp = x_f_freq
x_f_freq = x_f_freq[x_f_freq >= 0]
x_f = x_f[temp >= 0]

# Plot the amplitude spectrum in logarithmic scale
plt.plot(x_f_freq / 1000, 20 * np.log10(np.abs(x_f)))
plt.xlim(0, 50)
plt.xlabel('f (kHz)')
plt.ylabel('|X(f)| dB')
plt.grid(True)
plt.show()
```
$\endgroup$
3
  • $\begingroup$ If you post code, please make sure it actually runs. There are no imports and generated_signal is undefined. It would also help if you explain your modulation scheme up front. $\endgroup$
    – Hilmar
    Commented Dec 2, 2023 at 14:02
  • $\begingroup$ Sorry Corrected it now :) $\endgroup$
    – 3d014
    Commented Dec 2, 2023 at 14:18
  • $\begingroup$ What does that even mean. I'm not familiar with this network .... $\endgroup$
    – 3d014
    Commented Dec 2, 2023 at 16:59

1 Answer 1

1
$\begingroup$

Your professor, just like your last question, seems to be providing you with erroneous prompts.

His plot shows $\tt{dB}$ values on the y-axis of the FFT plot, but these are actually linear.

With that being said, there is no error per se in your code, but you need to scale the FFT result:

plt.plot(x_f_freq / 1000, 2*np.abs(x_f)/len(x_t))

The reason for the factor of 2 is that we want to compensate plotting the one-sided spectrum, since the energy is split between the positive and negative frequencies. The 1/len(x_t) factor is a standard scaling for the forward FFT: $$X[k] = \frac{1}{N}\sum_{n=0}^{N-1}x[n]e^{-j2\pi k n/N}$$

This is what I get: enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.