# Clear the Workspace rm (list=ls()) # Generate a 5 second 1 kHz signal at 44.1 kHz sampling f <- 500 # central frequency amp <- 26213 # amplitude of the wave tau <- 0.015 # fade in and out time constant time <- 5 # time duration in seconds rate <- 44100 # audio file sample rate len <- time *rate # length of the file rg <- 100 # range of random values about zero # Increment time samples at 44.1 kHz t <- seq(rate*time)/rate # time increments # Generate a random frequency spectrum # First value is zero so the mean of the transform is zero # Second half of the series is an inverted order complex conjugate to insure real values # The middle value is not duplicated and is real, so [1] and [rate*time/2+1] are not copied w1 <- runif(rate*time/2+1, min=-rg, max=rg) + runif(rate*time/2+1, min=-rg, max=rg)*1i w1[1] <- 0.0 w1[rate*time/2+1] <- runif(1, min=-rg, max=rg) w2 <- Conj(rev(w1[2:(rate*time/2)])) w <- c(w1, w2) # Set values for an octave bandpass about the central frequency df <- 1/time # Frequency resolution nl <- as.integer(f/sqrt(2)/df) # Location of low end of pass band nh <- as.integer(f*sqrt(2)/df) # Location of high end of pass band # Remove the lower frequencies w[2:(nl+1)] <- 0 w[(length(w)-(nl-1)):length(w)] <- 0 # Remove the higher frequencies w[(nh+1):(length(w)-(nh-1))] <- 0 # Inverse FFT to generate white noise u <- fft( w, inverse=TRUE) # Rescale the amplitude u1 <- as.real(u) u2 <- u1*amp/max(abs(u1)) final <- as.integer( u2 ) # Analyze the results # plot( final, pch="." ) # lines( c(1, length(final)), c(0,0) ) lwin <- 1 hwin <- 10000 ff <- Mod( fft( final )[1:(length(final)/2)] ) feq <- (seq(length(ff))-1)*rate/length(final) plot( feq[lwin:hwin], ff[lwin:hwin] ) # Check the frequencies octave bandwidth wl <- f/sqrt(2) wh <- f*sqrt(2) lines( c(wl,wl), c(0,max(ff)), col=2, lwd=2) lines( c(wh,wh), c(0,max(ff)), col=2, lwd=2) # Fade the signal in and out audio <- u2*(1-exp(-t/tau))*(1-exp(-(time-t)/tau)) # Append zero a 1/2 second before and 1/2 second after t1 <- seq(rate/2)*0 final <- as.integer(c(t1, audio, t1)) # Save the file to White500.txt write( final, file="White500.txt", ncolumns = 1 ) # play( final )