2015-08-20 28 views
3

我對整個R-thing仍然很陌生。R:如何產生嘈雜的正弦函數

我有以下目的;我有一個正弦函數,描述隨時間變化的鈣顆粒數量: 類似於y = a * sin(b * t)+ c

因爲實際上隨機事件中描述了鈣的生成和去除,喜歡爲我的功能添加隨機噪聲項(最好在平均噪聲幅度中可縮放)。

類似於z = y + random *振幅

你能幫我嗎?

最佳

回答

1

y <- jitter(a*sin(b*t) + c)使用jitter()功能將隨機噪聲添加到您的功能。您可以指定jitter()中的「量」參數來控制幅度。

+1

是不是'抖動()'意爲了可視化的目的而產生小的噪音(避免重複的數據點混淆彼此)? – ecerulm

8

這裏有一個方法,我會用 - 我提供了關於如何可能產生的誤差(均勻分佈VS高斯分佈)兩個選項:

### 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