Compute EDF Signal Averages

Compute EDF Signal Averages

In this tutorial, you will learn how to:

  1. Load a signal and compute signal averages.

The Requirements for running this tutorial are described after the discussion of the tutorial script.

The script

require 'rubygems'
require 'colorize'
require 'edfize'
require 'nsrr'
require 'nsrr/commands/download' learn/polysomnography/edfs/learn-nsrr01.edf --token=public))

puts "EDFs Downloaded: #{Edfize.edf_paths.count}"

Edfize.edfs do |edf|
  puts "\nEDF: #{edf.filename}"
  puts "               Signal Average"
  edf.signals.each do |signal|
    physical_values_average = signal.physical_values.inject(&:+) / signal.physical_values.count
    puts "     #{format("%16s", signal.label)} #{physical_values_average.round(2).to_s.colorize(:green)} #{signal.physical_dimension}"

puts "\nCongrats! You have completed your fourth tutorial!".colorize(:green).on_white

download script

Breaking down the script

The script loads an EDFs from the NSRR using the method, described in Tutorial 01.

Next, the script iterates through each EDF in the current folder or subfolders, and loads all the signals into memory using the load_signals method. Note that for large EDFs, you may choose to load the signals epoch by epoch using the load_epoch(epoch_number, epoch_size) method instead.

physical_values_average = signal.physical_values.inject(&:+) / signal.physical_values.count

For each signal in the EDF, the script then computes the average value of the physical values, by summing up all the values, and then dividing by the number of values in the signal.

Try These

  1. Modify the script to compute the minimum and maximum of each signal. Ruby has two array methods max and min that can be used for this purpose.

  2. As noted, this script doesn't handle large EDF files well as the load_signals method attempts to load every value of every signal into memory. Modify the script to compute the averages by loading epoch by epoch instead.

If you completed this tutorial, please leave a review and check out the next tutorial in the series:

Tutorial 05 - Counting Apneas in EDF Annotations


This tutorial requires that you have Ruby 2.2 or above installed, preferably Ruby 2.3 or higher.


Extract the script to your desktop to a folder of the same name. Open your console (Command Prompt on Windows or Terminal on Mac/Linux) and navigate to your tutorial folder.


cd ~/Desktop/tutorial_04


cd %USERPROFILE%\Desktop\tutorial_04


  • cd stands for "Change Directory"
  • ~, and %USERPROFILE% represents your user home directory

Installing the required gems.

This script requires you to have the colorize, nsrr, and edfize gems installed. The colorize gem is used to color command line output. The nsrr gem is used to easily download sets of files from the National Sleep Research Resource ( The edfize gem is used to read and write EDF files using Ruby.

In your console type:

gem install colorize nsrr edfize --no-document

This command will get you the latest stable release of the colorize, nsrr, and edfize gem from The --no-document flag specifies to skip the installation of the documentation for the gems.

Running the script.

In your console and from the tutorial_04 directory, type:

ruby tutorial_04.rb


Remember, if you have any questions on this script, please let us know on the NSRR forum here:, or email us at