Skip to content

Instantly share code, notes, and snippets.

@kalesan
Created August 3, 2021 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kalesan/e1e5028676be12a86e7a4ae91134e951 to your computer and use it in GitHub Desktop.
Save kalesan/e1e5028676be12a86e7a4ae91134e951 to your computer and use it in GitHub Desktop.
import cvxpy as cp
import numpy as np
# Number of channels
N = 10
N0 = 1 # Normalized noise level
SNR_dB = 10 # The signal to noise ratio in dB
P = 10**(SNR_dB/10) # Sum power budget defined via the SNR
# The channel specific gains drawn from Gaussian distribution
g = np.abs(np.random.randn(N, 1))
G = np.diag(g[0:,0]) # Make gains a diagonal matrix
# Problem construction
# The individual power allocations as variables (N x 1 real vector)
p = cp.Variable(N)
objective = cp.Maximize(cp.sum(cp.log(1 + (G @ p) / N0)))
constraints = []
constraints.append(p >= 0) # Positive powers constraint
constraints.append(cp.sum(p) <= P) # Total power budget
problem = cp.Problem(objective, constraints)
# Solve the problem
result = problem.solve()
# Print the optimal sum rate
# This has now natural log basis making the unit nats
print('Sum-rate (capacity) in nats', result)
# Show the power allocation
print('Power allocation', p.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment