Code covered by the BSD License  

Highlights from
Oscillator and Signal Generator

4.7 | 3 ratings Rate this file 48 Downloads (last 30 days) File Size: 15.2 KB File ID: #37376 Version: 1.8
image thumbnail

Oscillator and Signal Generator


Owen Brimijoin (view profile)


02 Jul 2012 (Updated )

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

| Watch this File

File Information

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.
  Input arguments:
    wavetype (case insensitive string):
     'Reverse Sawtooth'
     'Linear Sweep'
     'Log Sweep'
     'FM signal' or 'fm'
     'Click Train' or 'click'
     'White Noise' or 'white'
     'Octave Band' or 'octave'
     'Half Octave' or 'half'
     'Third Octave' or 'third'
     'Quarter Octave' or 'quarter'
     'Brown Noise' or 'brown'
     'Pink Noise' or 'pink'
     'Blue Noise' or 'blue'
     'Violet Noise' or 'violet'
     'Grey Noise' or 'grey'
     'Speech Noise' or 'speech'
    duration (in seconds)
    frequency (in Hz)
        NOTE: linear and log sweeps require [start stop] frequency vector
              'fm signals' require a num_samples-long vector of frequencies.
  Optional input arguments:
    gate (in seconds): duration of a raised cosine on/off ramp
    phase (in radians): starting phase of the waveform.
    sample_rate (in samples): 44100 is default, custom rates are possible
    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('FM',1,freq_vector); % signal changing in freq over time
    wave = oscillator('White Noise',1,[],0.1); %ramped on and off noise
    wave = oscillator('Half Octave',1,440); %half octave noise, 440 Hz centre.
    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 to a
  peak absolute value of 1.0
  For the 'FM signal' option, the function should be provided with a vector
  of frequencies (in Hz) that is the same duration and sample frequency as
  the desired signal. Failing this, the function will *attempt* to resample
  the frequency vector using spline interpolation. YMMV.
  Note that the 'grey noise' is *generic* grey and is based the ISO 66-phon
  Equal-loudness contour.
(c) W. Owen Brimijoin - MRC Institute of Hearing Research
Tested on Matlab R2011b and R14

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
28 Oct 2015 Owen Brimijoin

Owen Brimijoin (view profile)


Excellent suggestion. I will see what I can do about that. In the meantime, there's an awfully simple thing you could do to make swept square waves:

- generate a swept sine, then do this:

wave(wave>=0) = 1;
wave(wave<0) =- 1;

Watch this space for swept triangle and sawtooth waveforms.

Comment only
27 Oct 2015 Carl Witthoft

Currently only sinusoidal patterns can be chirped. Any thought to allowing square, triangle & sawtooth, for examples, to be swept as well?

03 Mar 2015 Paul

Paul (view profile)

Thank you!

28 Jun 2013 Owen Brimijoin

Owen Brimijoin (view profile)

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.

Comment only
24 Jun 2013 Owen Brimijoin

Owen Brimijoin (view profile)

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!

Comment only
06 Jun 2013 Deborah

Deborah (view profile)

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.

03 Jul 2012 1.1

Additional commenting for clarity.

30 Jul 2012 1.2

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

15 Jan 2013 1.3

Fixed a bug related to the generation of short signals.

28 Jun 2013 1.4

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

08 Jul 2013 1.5

The function now uses a somewhat slower but far more accurate means of generating spectrally shaped noises (ifft). The function also now includes blue noise and violet noise.

06 May 2014 1.6

Version 1.4 06/05/14 - added octave (and fractions) noise options.

07 May 2014 1.7

Added functionality: the function is now able to generate signals that vary continuously in frequency according to a supplied freq_vector argument.

07 Jul 2014 1.8

Updated tags

15 Oct 2015 1.8

Fixed a bug in which long duration speech noise signals were shifted down in frequency

15 Oct 2015 1.8

Changed function title on FileX

Contact us