KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

PLAYING A TONE

Playing a simple tone in MATLAB is quite easy. However, if you are really in need of excellent timing accuracy for sound I would strongly recommend read THIS.

Let's start a new script:


clc;
clear all;
close all;

rng('shuffle');

To play a beep, you simply use the following:


Beeper(400,0.3,0.1);

This command plays a simple tone with a frequency of 400 Hz for 0.1 seconds. The 0.3 is a normalized volume value that can be set between 0 and 1.

​However, if you want accurate timing accuracy as note above you have to handle this a bit differently. There is quite a bit here so I will add comments with each line so you know what it does.

Try the following script:


clc;
clear all;
close all;

rng('shuffle');

% this loads a default sample audio file in MATLAB, a piece of music by Handel.

load handel;
% once loaded, you will note it creates two variables, Fs and y. Fs is the sampling frequency of the music. Y % is a vector of the amplitudes of the signal.

% PYSCHTOOLBOX needs the music to be in a row vector so we have to transpose the matrix y
y = y';

% figure out how many audio channels there are

nrchannels = size(y,1);

% perform basic initialization of the sound driver:
InitializePsychSound;

% try to open an audio port for playing
audio_port = PsychPortAudio('Open', [], [], 0, Fs, nrchannels);

% put the music into an audio buffer - a special place in memory where it will be stored
PsychPortAudio('FillBuffer', audio_port, y);


% start playing what is in the audio buffer
% start_time is simply when the music starts playing
start_time = PsychPortAudio('Start', audio_port, 0, 0, 1);


% wait some time so the music can play
WaitSecs(5);


% Stop playback
PsychPortAudio('Stop', audio_port);

% Close the audio port
PsychPortAudio('Close', audio_port);

This code is also HERE. There is quite a bit here but playing sound if you want to ensure very precise timing accuracy is not easy. At the end of this day, all this code does is play whatever is in y at a frequency of Fs.

​Other Activities
1. Try changing Fs manually to see what this does to the music.
2. Try changing what is in y to create your own music. 

In MATLAB, you can read in any wav file and play it using these commands. You will have to convert the wav file to a MATLAB file by using:

[y,Fs] = audioread(filename);

Now, onto the next tutorial!
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops