Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to generate a noisy sine function

I am still pretty new to the whole R-thing.

I have the following aim; I have a sine function that describes a calcium particle number over time: something like y = a * sin (b*t) + c

Since in reality the generation and removal of calcium is described in stochastic events, I would like to add a random noise term to my function (preferably scalable in average noise amplitude).

Something like z = y + random*Amplitude

can you help me out?

Best

like image 394
Arne Avatar asked Aug 20 '15 07:08

Arne


People also ask

How do you make a sine wave function?

In its most general form, the sine wave can be described using the function y=a*sin⁡(bx), where: a is known as the amplitude of the sine wave. b is known as the periodicity.

What is sinusoidal noise?

Sinusoidal Noise is a modular light installation that uses random oscillating patterns to create a larger sense of movement. The work comprises 98 pixels each of which fades on and off at a unique frequency.

How do you control the frequency of a sine wave?

To change the frequency of a sine wave generated by the “osc~” object you need to send the frequency in Hz to the hot inlet. To control the phase of a sine wave you can set it on the right inlet of osc~. This will set the phase of the repeating waveform; any new input will reset the phase.


2 Answers

y <- jitter(a*sin(b*t) + c) using the jitter() function will add random noise to your function. you can specify the "amount" parameter inside jitter() to control the amplitude.

like image 61
Gaurav Avatar answered Sep 29 '22 16:09

Gaurav


Here's an approach I would use - I provided two options on how your error might be generated (uniform distribution vs Gaussian distribution):

### Equation: y=a*sin(b*t)+c.unif*amp
# variables
n <- 100 # number of data points
t <- seq(0,4*pi,,100)
a <- 3
b <- 2
c.unif <- runif(n)
c.norm <- rnorm(n)
amp <- 2

# generate data and calculate "y"
set.seed(1)
y1 <- a*sin(b*t)+c.unif*amp # uniform error
y2 <- a*sin(b*t)+c.norm*amp # Gaussian/normal error

# plot results
plot(t, y1, t="l", ylim=range(y1,y2)*c(1,1.2))
lines(t, y2, col=2)
legend("top", legend=c("y1", "y2"), col=1:2, lty=1, ncol=2, bty="n")

enter image description here

like image 33
Marc in the box Avatar answered Sep 29 '22 16:09

Marc in the box