Skip to main content
added 88 characters in body
Source Link
Seanny123
  • 1.5k
  • 3
  • 18
  • 37

I currently have this code to generate a discrete signal with random amplitudes, but a fixed frequency:

import matplotlib.pyplot as plt
import numpy as np

# constants
t_len = 1.0.5
freq = 10.0
dt = 0.001

# set the see for reproducible results
rng = np.random.RandomState(0)

# create the random values to interpolate between
assert dt < freq
white_size = int(t_len * freq)
white_vals = rng.uniform(low=-1, high=1, size=int(t_len*freq)size=white_size)

# setup the interpolation
white_noise = np.zeros(int(t_len / dt))
assert white_vals.shape[0]white_size < white_noise.shape[0]

# how can I avoid using this for-loop?
for w_i in xrange(white_vals.shape[0]white_size):
    white_noise[int(w_i/freq/dt):int((w_i+1)/freq/dt)] = white_vals[w_i]

# plot the result for visual verificaition
plt.plot(white_noise)
plt.show()

It generates something like this:

sig_plots

Like it says in the code comments, how do I get rid of this for-loop and vectorize the operation? I feel like I should be using reshape to accomplish this, but I'm not sure how.

I currently have this code to generate a discrete signal with random amplitudes, but a fixed frequency:

import matplotlib.pyplot as plt
import numpy as np

# constants
t_len = 1.0
freq = 10.0
dt = 0.001

# set the see for reproducible results
rng = np.random.RandomState(0)

# create the random values to interpolate between
assert dt < freq
white_vals = rng.uniform(low=-1, high=1, size=int(t_len*freq))

# setup the interpolation
white_noise = np.zeros(int(t_len/dt))
assert white_vals.shape[0] < white_noise.shape[0]

# how can I avoid using this for-loop?
for w_i in xrange(white_vals.shape[0]):
    white_noise[int(w_i/freq/dt):int((w_i+1)/freq/dt)] = white_vals[w_i]

# plot the result for visual verificaition
plt.plot(white_noise)
plt.show()

It generates something like this:

Like it says in the code comments, how do I get rid of this for-loop and vectorize the operation? I feel like I should be using reshape to accomplish this, but I'm not sure how.

I currently have this code to generate a discrete signal with random amplitudes, but a fixed frequency:

import matplotlib.pyplot as plt
import numpy as np

# constants
t_len = 0.5
freq = 10.0
dt = 0.001

# set the see for reproducible results
rng = np.random.RandomState(0)

# create the random values to interpolate between
assert dt < freq
white_size = int(t_len * freq)
white_vals = rng.uniform(low=-1, high=1, size=white_size)

# setup the interpolation
white_noise = np.zeros(int(t_len / dt))
assert white_size < white_noise.shape[0]

# how can I avoid using this for-loop?
for w_i in xrange(white_size):
    white_noise[int(w_i/freq/dt):int((w_i+1)/freq/dt)] = white_vals[w_i]

# plot the result for visual verificaition
plt.plot(white_noise)
plt.show()

It generates something like this:

sig_plots

Like it says in the code comments, how do I get rid of this for-loop and vectorize the operation? I feel like I should be using reshape to accomplish this, but I'm not sure how.

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Seanny123
  • 1.5k
  • 3
  • 18
  • 37

Generate a random discrete signal

I currently have this code to generate a discrete signal with random amplitudes, but a fixed frequency:

import matplotlib.pyplot as plt
import numpy as np

# constants
t_len = 1.0
freq = 10.0
dt = 0.001

# set the see for reproducible results
rng = np.random.RandomState(0)

# create the random values to interpolate between
assert dt < freq
white_vals = rng.uniform(low=-1, high=1, size=int(t_len*freq))

# setup the interpolation
white_noise = np.zeros(int(t_len/dt))
assert white_vals.shape[0] < white_noise.shape[0]

# how can I avoid using this for-loop?
for w_i in xrange(white_vals.shape[0]):
    white_noise[int(w_i/freq/dt):int((w_i+1)/freq/dt)] = white_vals[w_i]

# plot the result for visual verificaition
plt.plot(white_noise)
plt.show()

It generates something like this:

Like it says in the code comments, how do I get rid of this for-loop and vectorize the operation? I feel like I should be using reshape to accomplish this, but I'm not sure how.