Code covered by the BSD License  

Highlights from
Oscillator and Signal Generator

5.0

5.0 | 1 rating Rate this file 57 Downloads (last 30 days) File Size: 4.14 KB File ID: #37376
image thumbnail

Oscillator and Signal Generator

by W. Owen Brimijoin

 

02 Jul 2012 (Updated 28 Jun 2013)

A simple command-line function for generating standard waveforms, click trains and noise bursts.

| Watch this File

File Information
Description

Given a duration and a frequency, this function can rapidly generate signals of different waveform types. The user may also optionally gate the signal on and off with a raised cosine ramp, as well as specify the starting phase and/or sample frequency.

OSCILLATOR(wavetype,duration,frequency) Input arguments:

  wavetype (string):
   'Sinusoid'
   'Triangle'
   'Square'
   'Sawtooth'
   'Reverse Sawtooth'
   'Linear Sweep'
   'Log Sweep'
   'Click Train'
   'White Noise'
   'Pink Noise'
   'Brown Noise'
   'Grey Noise'
   'Speech Noise'

  duration (in seconds)
  frequency (in Hz)
      NOTE: linear and log sweeps require [start stop] frequency vector

Optional input arguments:
 OSCILLATOR(wavetype,duration,frequency,gate,phase,sample_freq)
  gate (in seconds): duration of a raised cosine on/off ramp
  phase (in radians): starting phase of the waveform.
  sample_freq (in samples): 44100 is default, custom rates are possible

Examples:

  wave = OSCILLATOR('Sinusoid',1,1000); % simple pure tone at 1000 Hz.
  wave = OSCILLATOR('Sawtooth',2,440); % 2 second sawtooth at 440 Hz.
  wave = OSCILLATOR('Pink Noise',1); % 1 second of pink (1/F) noise
  wave = OSCILLATOR('Linear Sweep',2,[440 880]); % linear sweep from 440 to 880 Hz.
  wave = OSCILLATOR('Log Sweep',2,[20 20000],.01); % ramped on/off log sweep.
  wave = OSCILLATOR('White Noise',1,[],0.1); %ramped on and off noise
  wave = OSCILLATOR('Sinusoid',1,220,0,pi/2,48000); %pure tone with a
           starting phase of 90 degrees and sample rate set to 48000.

Omitting 'wavetype' sets it to sinusoid, omitting 'duration' sets it to
one second, and omitting 'frequency sets it to 440 Hz. Gate is set to 0,
phase to 0 and sample rate to 44100; All output waves are scaled from -1
to 1.

Note that the noise signals (pink, brown, grey, etc) are only
approximations arrived at through filtering, rather than through the more
accurate iterative processes found elsewhere. Suggestions for better (but
just as fast) methods or improved coefficients are heartily welcomed.

(c) W. Owen Brimijoin - MRC Institute of Hearing Research
Tested on Matlab R2011b and R14
Version 1.0 18/05/12 - original
Version 1.1 29/06/12 - added pink and speech-shaped noise options
Version 1.2 26/06/13 - added swept sine and brown and grey noise options

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
28 Jun 2013 W. Owen Brimijoin

Oops.. my mistake, "chirp.m" is only included in the Signal Processing toolbox! I have updated the function to include simple linear and logarithmic sweep options. For these you need to provide a [start stop] vector of frequencies.

To the list of signal options, I have also added brown noise (1/f^2) and grey noise. Mind you, it's a generic grey (ISO Equal-Loudness at 60 phons), since *true* grey noise must be custom defined to match each listener's psychoacoustic loudness contour.

The updated version has been submitted and should pop up soon.

24 Jun 2013 W. Owen Brimijoin

I am so glad you find this submission helpful, Deborah. Excellent suggestions, I will certainly add more noise forms as soon as I can compute the best filter coefficients I can. Watch this space for grey noise and brown noise, among others.

I am, however, hesitant to add support for frequency sweeps because I think it would complicate the currently simple input argument structure. What's more, there is the built-in Matlab 'chirp.m' function that does a bang-up job of making fm signals!

06 Jun 2013 Deborah

I really like this submission, saved me an awful lot of trouble!
However, while you're at it, I would love if there were more waveforms available, like a swept sine (linear sweep, exponential... but I think this does exist somewhere on FE) or different noise forms like grey noise (weighted with an inverse A-Filter).
The function is documented nicely and has a helpful help section.

Updates
03 Jul 2012

Additional commenting for clarity.

30 Jul 2012

Fixed a bug related to the user specifying 'frequency' while selecting noise as a signal type.

15 Jan 2013

Fixed a bug related to the generation of short signals.

28 Jun 2013

Function has been updated to include options for generating linear and logarithmic frequency sweeps, as well as brown and grey noise options

Contact us