## Change this to whatever subfolder of the main directory contains the files to analyze
folder <- "Vibration Ground Test 2-24"
## Keep character vector of all files in subfolder
fileNames <- list.files(path = folder, pattern="MAD.....JPG")
## Create an "Images" directory if one does not already exist in the working directory.
if(!dir.exists(paste(folder,"/Images",sep=""))){
dir.create(paste(folder,"/Images",sep=""))
}
## Create 2 data frames to store data from files.
allData <- as.data.frame((1:1024)/100) ## Will store raw data values converted to integers.
freqData <- data.frame(1) ## Will store peak frequency data for each file
## Changes column titles in data frame.
names(allData) <- c("Time")
MAD0044 | MAD0045 | MAD0046 | MAD0047 | MAD0048 | MAD0049 | MAD0050 | MAD0051 | MAD0052 | MAD0053 | |
---|---|---|---|---|---|---|---|---|---|---|
Peak Freq (Hz) | 10.15625 | 14.45312 | 14.45312 | 19.92188 | 19.92188 | 25 | 25 | 0 | 5.078125 | 5.078125 |
## Go file by file and ...
for(fileName in fileNames){
## Convert raw values to integers and store in a dataframe
raw <- readRaw(paste(folder,"/",fileName,sep=""))
data <- strtoi(raw$fileRaw,16L)
data <- (as.numeric(data) - 127.5)/127.5
df <- as.data.frame(cbind((1:length(data))/100.0, data))
## Append newly analyzed raw data to allData data frame
allData <- cbind(allData,as.numeric(data[1:1024]))
names(allData)[length(names(allData))] = sub(".JPG","",fileName)
## Append new frequency data to freqData data frame and generate PNG File
spec <- meanspec(allData[,names(allData)==sub(".JPG","",fileName)],f=200, plot=FALSE) ## Performs FFT assuming 200 Hz was the recording frequency of the data
## Create images for each data file of data plotted over time and FFT information
png(paste(paste(folder,"/Images/",sep=""),sub(".JPG","",fileName),".png",sep=""), height = 480, width = 480, bg = "white") ## Create png file
par(mfrow = c(2,1))
plot(allData$Time,data[1:1024], type = "n",xlab = "Time (s)", ylab = paste(sub(".JPG","",fileName)," ACCELERATION (m/s/s)",sep = "")) ## Create plot of data
lines(allData$Time,data[1:1024])
minAmplitude <- .7
peaks <- fpeaks(spec, nmax = 6, plot=TRUE, labels = FALSE, legend = FALSE, title = FALSE, main = paste("Frequencies present in ", fileName, sep="")) ## Finds peak frequencies of the data and plot them
if(all(peaks[,2] < minAmplitude)){
drivingFreqKilo = 0
drivingFreqAmp = .95
}
else{
drivingFreqKilo <- peaks[peaks[,2] == max(peaks[,2]),1]
if(any(5 == round(1000*peaks[,1],0))){
drivingFreqKilo <- peaks[5 == round(1000*peaks[,1],0),1]
drivingFreqAmp <- .5
}
else{
drivingFreqAmp <- peaks[peaks[,1] == drivingFreqKilo,2]
}
}
text(.05, .85, cex = 1.2, col = "red",paste("Driving Frequency = ",round(1000*drivingFreqKilo,2), " Hz", sep = ""))
dev.off()
freqData <- cbind(freqData,1000*drivingFreqAmp) ## 1000*peaks is used because frequencies are given in kHz.
names(freqData)[length(names(freqData))] = sub(".JPG","",fileName) ## Change column names of data frame to file names
}
… and here they are!