Answers.Body: <p>Not so sure if you mean one singe value per file, that averaged the whole spectrum?? If so, here is a code that should make the trick:</p>
  <pre><code>
  library(seewave)
  library(tuneR)
  
  audio_files &lt;- list.files(&quot;you_files_path_name&quot;, full.names = TRUE, pattern = &quot;*.WAV&quot;)
  
  #### PSD_function
  calculate_psd &lt;- function(file) {
    
    # read file
    snd &lt;- readWave(file)
    
    # calculate mean spectrum
    psd &lt;- as.data.frame(meanspec(snd, plot = F, PSD = F)) # (just because it was faster to run, change back if necessary)
    
    # name columns
    colnames(psd) &lt;- c(&quot;frequency_Hz&quot;, &quot;PSD&quot;)
    
    # calculate mean PSD (is this what you wanted??)
    m_psd = mean(psd$PSD)
    
    # return value
    return(m_psd) 
    }
  
  
  ###########################################################
  i=1
  
  # repeat (number of files) times... 
  list_PSD_all_files = lapply(1:length(audio_files), function(i){ 
    # select file name number i
    file &lt;- audio_files[i]
    
    # read file, calculate PSD
    psd &lt;- calculate_psd(file) 
    
    # save in a data. frame
    PSD_per_file = data.frame( file_name = file,
                               mean_PSD = psd)
    return(PSD_per_file)
  })
  
  # transform your list into a data frame
  PSD_all_files = do.call(rbind, list_PSD_all_files)
  
  # save csv file
  write.csv(PSD_all_files, &quot;your_results_path_name/MeanPSD_per_file.csv&quot;)
  </code></pre>
  <p>If not, then adjust what is in the function <code>calculate_psd</code>.</p>
  <p>Hope that helps!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-02T20:19:33.160Z
Answers.Text: Thank you very much lframond!! I will try this code. all the best! Martin.
Answers.UserDisplayName: Martin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-01T14:16:37.133Z
Answers.LastActivityDate: 2023-06-01T14:16:37.133Z
Answers.UserDisplayName: lframond
Answers.Body: <p>If you really wanted the mean PSD of a file, then keep the Parseval's theorem in mind</p>
  <ul>
  <li>PSD (power spectral density) is (Power/Hz) and the integral is simply Power</li>
  <li>this is equal to the power integral in the time domain</li>
  <li>the mean PSD is then the overall RMS value of your data in the file</li>
  <li>this can also be estimated also in time domain</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-02T05:10:31.933Z
Answers.LastActivityDate: 2023-06-02T05:10:31.933Z
Answers.UserDisplayName: WMXZ
Body: <p>I'm trying to calculate a mean PSD value from each .wav file inside a folder with many sound files.  I'm using seewave to calculate this but having trouble finding a nice result for my further analysis. This info will be used to characterize an urban soundscape coupled with other acoustic indices.</p>
  <p>here is my R code:</p>
  <p>library(seewave)</p>
  <p>audio_files &lt;- list.files(&quot;path_of_my_files&quot;, full.names = TRUE)</p>
  <h1>PSD_function</h1>
  <p>calculate_psd &lt;- function(file) {</p>
  <p>snd &lt;- readWave(file)</p>
  <p>psd &lt;- spec(snd, plot = FALSE, PSD = TRUE)</p>
  <p>return(psd)
  }</p>
  <h1>create a list</h1>
  <p>psd_list &lt;- list()</p>
  <h1>calculate PSD for each file</h1>
  <p>for (file in audio_files) {
  psd &lt;- calculate_psd(file)
  psd_list[[file]] &lt;- psd
  }</p>
  <p>Here is where I get lost. How do I calculate and save the mean PSD value for each .wav file?</p>
  <p>If anyone can help me with this I will be very much appreciated!!</p>
  <p>all be best,</p>
  
Tags: <r><power-spectral-density>
Title: PSD calculation using seewave
ThreadText: # PSD calculation using seewave
  
  (Asked by: username_0 on 2023-05-31)
  
  I'm trying to calculate a mean PSD value from each .wav file inside a folder with many sound files. I'm using seewave to calculate this but having trouble finding a nice result for my further analysis. This info will be used to characterize an urban soundscape coupled with other acoustic indices.
  
  here is my R code:
  
  library(seewave)
  
  audio\_files <- list.files("path\_of\_my\_files", full.names = TRUE)
  
  # PSD\_function
  
  calculate\_psd <- function(file) {
  
  snd <- readWave(file)
  
  psd <- spec(snd, plot = FALSE, PSD = TRUE)
  
  return(psd)
  }
  
  # create a list
  
  psd\_list <- list()
  
  # calculate PSD for each file
  
  for (file in audio\_files) {
  psd <- calculate\_psd(file)
  psd\_list[[file]] <- psd
  }
  
  Here is where I get lost. How do I calculate and save the mean PSD value for each .wav file?
  
  If anyone can help me with this I will be very much appreciated!!
  
  all be best,
  
  **Tags:** r, power-spectral-density
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-06-01. Score: 3
  
  Not so sure if you mean one singe value per file, that averaged the whole spectrum?? If so, here is a code that should make the trick:
  
  library(seewave)
  library(tuneR)
  
  audio_files <- list.files("you_files_path_name", full.names = TRUE, pattern = "*.WAV")
  
  #### PSD_function
  calculate_psd <- function(file) {
    
    # read file
    snd <- readWave(file)
    
    # calculate mean spectrum
    psd <- as.data.frame(meanspec(snd, plot = F, PSD = F)) # (just because it was faster to run, change back if necessary)
    
    # name columns
    colnames(psd) <- c("frequency_Hz", "PSD")
    
    # calculate mean PSD (is this what you wanted??)
    m_psd = mean(psd$PSD)
    
    # return value
    return(m_psd) 
    }
  
  
  ###########################################################
  i=1
  
  # repeat (number of files) times... 
  list_PSD_all_files = lapply(1:length(audio_files), function(i){ 
    # select file name number i
    file <- audio_files[i]
    
    # read file, calculate PSD
    psd <- calculate_psd(file) 
    
    # save in a data. frame
    PSD_per_file = data.frame( file_name = file,
                               mean_PSD = psd)
    return(PSD_per_file)
  })
  
  # transform your list into a data frame
  PSD_all_files = do.call(rbind, list_PSD_all_files)
  
  # save csv file
  write.csv(PSD_all_files, "your_results_path_name/MeanPSD_per_file.csv")
  
  If not, then adjust what is in the function calculate_psd.
  Hope that helps!
  
  > **Comments:**
  > **username_0**: Thank you very much username_1!! I will try this code. all the best! username_0.  
  
  ---
  
  ### Answer by username_2 on 2023-06-02. Score: 0
  
  If you really wanted the mean PSD of a file, then keep the Parseval's theorem in mind
  
  * PSD (power spectral density) is (Power/Hz) and the integral is simply Power
  * this is equal to the power integral in the time domain
  * the mean PSD is then the overall RMS value of your data in the file
  * this can also be estimated also in time domain
  
  
  ---
  
  