Guest User

Untitled

a guest
Nov 20th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #plotFFT_function
  2. #x: data_time_scale
  3. #y: data
  4. #samplingFreq: the sampling frequence of data capture device
  5. #dominant_frequency_order: defult first (1)
  6. #cex.axis: the size of head note for dominant frequencey
  7. #PSD : Power Spectral Density (modFFT**2[2:(N/2+1)]*2/N)
  8.  
  9. plotFFT <- function(x, y, samplingFreq, dominant_frequency_order=1,
  10. plotFreqs = (samplingFreq/2),
  11. cex.axis = 1, PSD = F, shadeNyq=TRUE){
  12. getFFTFreqs <- function(Nyq.Freq, data){
  13. if ((length(data) %% 2) == 1) # Odd number of samples
  14. {
  15. FFTFreqs <- c(seq(0, Nyq.Freq, length.out=(length(data)+1)/2),
  16. seq(-Nyq.Freq, 0, length.out=(length(data)-1)/2))
  17. } else{
  18. FFTFreqs <- c(seq(0, Nyq.Freq, length.out=length(data)/2),
  19. seq(-Nyq.Freq, 0, length.out=length(data)/2))
  20. }# Even number
  21.  
  22. return (FFTFreqs)
  23. }
  24. if(PSD == F){
  25. Nyq.Freq <- samplingFreq/2
  26. FFTFreqs <- getFFTFreqs(Nyq.Freq, y)
  27. FFT <- fft(y)
  28. modFFT <- Mod(FFT)
  29. FFTdata <- data.frame(cbind(FFTFreqs, modFFT=round(modFFT,3)))
  30. plot(FFTdata[FFTdata$FFTFreqs>0 & FFTdata$FFTFreqs < plotFreqs ,], t="l", pch=20, lwd=2, cex=0.8, main="",
  31. xlab="Frequency (Hz)", ylab="Power")
  32.  
  33. FFTdata1<-FFTdata[FFTdata$FFTFreqs > 1,]
  34. dominant_frequency<-round(FFTdata1[order(FFTdata1$modFFT,decreasing=TRUE),]$FFTFreqs[1:5],2)
  35. # Period axis on top
  36. axis(3, cex.axis=cex.axis,
  37. labels = paste0("dominant_frequency:", dominant_frequency[dominant_frequency_order]),
  38. at=dominant_frequency[dominant_frequency_order])
  39. if (shadeNyq == TRUE)
  40. {
  41. # Gray out lower frequencies
  42. rect(dominant_frequency[dominant_frequency_order]+0.5, 0, dominant_frequency[dominant_frequency_order]-0.5, max(FFTdata[,2]), col="gray", density=30)
  43. }
  44. #ret <- list("freq"=FFTFreqs, "FFT"=FFT, "modFFT"=modFFT)
  45. return (dominant_frequency = dominant_frequency[dominant_frequency_order])
  46. }
  47. if(PSD == T){
  48. #add PSD_112017
  49. Nyq.Freq <- samplingFreq/2
  50. Nf = length(y)/2
  51. FFTFreqs <- seq.int(from=0, to=Nyq.Freq, length.out=Nf)
  52. FFT <- fft(y)
  53. modFFT <- Mod(FFT)
  54.  
  55. #add PSD_112017
  56. Se = modFFT**2
  57. S <- c(Se[2:(Nf+1)] * 2 / Nyq.Freq) # Finally, the PSD!
  58.  
  59. #plot PSD
  60. FFTdata <- data.frame(cbind(FFTFreqs, modFFT=round(S,3)))
  61. plot(FFTdata[FFTdata$FFTFreqs>0 & FFTdata$FFTFreqs < plotFreqs ,], t="l", pch=20, lwd=2, cex=0.8, main="",
  62. xlab="Frequency (Hz)", ylab="PSD")
  63. FFTdata1<-FFTdata[FFTdata$FFTFreqs > 1,]
  64. dominant_frequency<-round(FFTdata1[order(FFTdata1$modFFT,decreasing=TRUE),]$FFTFreqs[1:5],2)
  65. # Period axis on top
  66. axis(3, cex.axis=cex.axis,
  67. labels = paste0("dominant_frequency:", dominant_frequency[dominant_frequency_order]),
  68. at=dominant_frequency[dominant_frequency_order])
  69. if (shadeNyq == TRUE)
  70. {
  71. # Gray out lower frequencies
  72. rect(dominant_frequency[dominant_frequency_order]+0.5, 0, dominant_frequency[dominant_frequency_order]-0.5, max(FFTdata[,2]), col="gray", density=30)
  73. }
  74. return (dominant_frequency = dominant_frequency[dominant_frequency_order])}
  75.  
  76. }
Add Comment
Please, Sign In to add comment