Re: [R] Fourier Transform fft help
After extensive research I found some information hinting at my freq array being wrong. I thought that the data spanned -F/2 to F/2. It seems that I should only plot the first half of the fft vector? Can anyone confirm or shed some light on this? One other thing I thought the magnitude should be 10 for 10* sin(2*pi*10*t), my magnitudes are much higher. delic wrote: > > I wrote a script that I anticipating seeing a spike at 10Hz with the > function 10* sin(2*pi*10*t). > I can't figure out why my plots do not show spikes at the frequencies I > expect. Am I doing something wrong or is my expectations wrong? > > > require(stats) > layout(matrix(c(1,2,3), 3, 1, byrow = TRUE)) > #SETUP >n<- 256 #nextn(1001) gives next power 2 >F<- 100 #Hz -50 to 50 Hz >dt <- 1/F >T<- n*dt >df <- 1/T >t<- seq(0,T,by=dt) #also try ts function >t<-t[1:length(t)-1] >freq <- 5 #Hz > > #SIGNAL FUNCTION >y <- 10* sin(2*pi*10*t) #10*sin(2*pi*freq*t) > #FREQ ARRAY >#f <- seq(-F/2,F/2,by=df) > f <- F/2*seq(-1,1,length.out=n+1) >f<-f[1:length(f)-1] > #FOURIER WORK >Y <- fft(y) >mag <- sqrt(Re(Y)^2+Im(Y)^2) >phase <- atan(Im(Y)/Re(Y)) >Yr<- Re(Y) >Yi<- Im(Y) > > #PLOT SIGNALS >plot(t,y,type="l",xlim=c(0,T)) >plot(f,Re(Y),type="l") >plot(f,Im(Y),type="l") > -- View this message in context: http://www.nabble.com/Fourier-Transform-fft-help-tp25475063p25477281.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fourier Transform fft help
why not try spectrum (power spectrum) spectrum(y, log="no") the major spike is at 0.1 and 1/0.1 = 10Hz On Wed, Sep 16, 2009 at 12:13 PM, delic wrote: > > I wrote a script that I anticipating seeing a spike at 10Hz with the > function 10* sin(2*pi*10*t). > I can't figure out why my plots do not show spikes at the frequencies I > expect. Am I doing something wrong or is my expectations wrong? > > > require(stats) > layout(matrix(c(1,2,3), 3, 1, byrow = TRUE)) > #SETUP > n <- 256 #nextn(1001) gives next power 2 > F <- 100 #Hz -50 to 50 Hz > dt <- 1/F > T <- n*dt > df <- 1/T > t <- seq(0,T,by=dt) #also try ts function > t<-t[1:length(t)-1] > freq <- 5 #Hz > > #SIGNAL FUNCTION > y <- 10* sin(2*pi*10*t) #10*sin(2*pi*freq*t) > #FREQ ARRAY > #f <- seq(-F/2,F/2,by=df) > f <- F/2*seq(-1,1,length.out=n+1) > f<-f[1:length(f)-1] > #FOURIER WORK > Y <- fft(y) > mag <- sqrt(Re(Y)^2+Im(Y)^2) > phase <- atan(Im(Y)/Re(Y)) > Yr <- Re(Y) > Yi <- Im(Y) > > #PLOT SIGNALS > plot(t,y,type="l",xlim=c(0,T)) > plot(f,Re(Y),type="l") > plot(f,Im(Y),type="l") > -- > View this message in context: > http://www.nabble.com/Fourier-Transform-fft-help-tp25475063p25475063.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Fourier Transform fft help
I wrote a script that I anticipating seeing a spike at 10Hz with the function 10* sin(2*pi*10*t). I can't figure out why my plots do not show spikes at the frequencies I expect. Am I doing something wrong or is my expectations wrong? require(stats) layout(matrix(c(1,2,3), 3, 1, byrow = TRUE)) #SETUP n<- 256 #nextn(1001) gives next power 2 F<- 100 #Hz -50 to 50 Hz dt <- 1/F T<- n*dt df <- 1/T t<- seq(0,T,by=dt) #also try ts function t<-t[1:length(t)-1] freq <- 5 #Hz #SIGNAL FUNCTION y <- 10* sin(2*pi*10*t) #10*sin(2*pi*freq*t) #FREQ ARRAY #f <- seq(-F/2,F/2,by=df) f <- F/2*seq(-1,1,length.out=n+1) f<-f[1:length(f)-1] #FOURIER WORK Y <- fft(y) mag <- sqrt(Re(Y)^2+Im(Y)^2) phase <- atan(Im(Y)/Re(Y)) Yr<- Re(Y) Yi<- Im(Y) #PLOT SIGNALS plot(t,y,type="l",xlim=c(0,T)) plot(f,Re(Y),type="l") plot(f,Im(Y),type="l") -- View this message in context: http://www.nabble.com/Fourier-Transform-fft-help-tp25475063p25475063.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.