1
$\begingroup$

I have a bit of code in R that adds noise to an harmonic series according to a normal distribution:

harmonic_data <- c(0.6613882, 1.0730417, 0.1745770
                   , -0.9370355, -0.9045850, 0.2323085)
noisy_data=numeric()
for (i in 1:length(harmonic_data)){
  noisy_data=c(noisy_data,harmonic_data[i]+rnorm(1,mean=harmonic_data[i],sd=1.5))
}

But now I need to add noise using a Mixture Gaussian with this form:

xx <- seq(-5,5,by=.01)
plot(xx,0.25*dnorm(xx,-4,1.5)+0.75*dnorm(xx,0,1.5)+0.25*dnorm(xx,4,1.5)
     ,type="l",xlab="",ylab=""
     )

(With the distribution centered around each value in the harmonic series) I can plot the distribution alright but can't generate one random value from that distribution to add it to the harmonic data. I have tried this:

noisy_data=numeric()
for (i in 1:length(harmonic_data)){
  noisy_data=c(noisy_data,harmonic_data[i]+(0.25*rnorm(1,harmonic_data[i]-4,1.5)+
                                            0.75*rnorm(1,harmonic_data[i],1.5)+
                                            0.25*rnorm(1,harmonic_data[i]+4,1.5))

)}

But it doesn't seem to be doing what I want, the final distribution is a single normal again. I realize this is probably a very basic question but I can't seem to find the answer.

I would greatly appreciate any help. Thanks!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.