Leader

Saturday 13 August 2016

Generate sine wave/tone Matlab

Download
genTone.m


How-to
Generating tones or sine waves of specific frequencies in Matlab is conceptually simple, but generating the correct frequency over the correct time period at the correct sampling rate can be a bit confusing at first. This article deals mainly to generating sine wave tones, and genTone.m is designed to simplify this process, but the timing aspects also apply to any signal output from a digital system.


Monday 31 August 2015

Matlab plot formats and how to quickly save high quality figures

Download
hgx.m
See also New graphics in Matlab 2014b and 2015 and creating nicer figures in one line of code for ng.m and og.m

The new graphics system in Matlab 2014b makes creating publication quality graphs much easier, but it still requires a little effort. See ng.m and og.m for two handy functions to improve the appearance of plots within Matlab. However, another important consideration is how to get the figure out of Matlab without ruining it. This function, hgx, is a handler for Matlab's various save/export functions and is designed to facilitate saving in useful forms using as little typing as possible


Monday 25 May 2015

Alternative function: Normal cumulative density function (normcdf)

Download
normcdf2.m

Details
This function implements the basic functionality of the normcdf function in Matlab's stats toolbox. Similar to normcdf it takes three inputs, the x axis (x), mean of the distribution (mu) and the standard deviation (sigma) and generates a normal cumulative density function using the equation:

 y = 0.5*(1+erf((x-mu)/sqrt(2*sigma^2)))

See also normpdf2 for the equivalent probability density distribution.

Example

% Define parameters
mu = -1; % Mean
sigma = 0.5; % Standard deviation
x = -10:0.1:10; % x axis

% Generate curve
y1 = normcdf2(x, mu, sigma);
y2 = normcdf2(x, mu-1, sigma+2);
y3 = normcdf2(x, mu+6, sigma+0.5);

% Plot 
figure
plot(x, [y1', y2', y3'])
ylabel('Prob.')
xlabel('x')
legend({...
    ['mu=', num2str(mu), ', sigma=', num2str(sigma)], ...
    ['mu=', num2str(mu-1), ', sigma=', num2str(sigma+2)], ...
    ['mu=', num2str(mu+6), ', sigma=', num2str(sigma+1)], ...
    })


Alternative function: Normal probability density function (normpdf)

Download
normpdf2.m

Details
This function implements the basic functionality of the normpdf function in Matlab's stats toolbox. Similar to normpdf it takes three inputs, the x axis (x), mean of the distribution (mu) and the standard deviation (sigma) and generates a normal probability density function:

y = 1/sqrt(2*pi*sigma^2) * exp(-(x-mu).^2 / (2*sigma^2))

See also normcdf2 for the equivalent cumulative distribution.

Example

% Define parameters
mu = -1; % Mean
sigma = 0.5; % Standard deviation
x = -10:0.1:10; % x axis

% Generate curve
y1 = normpdf2(x, mu, sigma);
y2 = normpdf2(x, mu-1, sigma+2);
y3 = normpdf2(x, mu+6, sigma+0.5);

% Plot 
figure
plot(x, [y1', y2', y3'])
ylabel('Prob.')
xlabel('x')
legend({...
    ['mu=', num2str(mu), ', sigma=', num2str(sigma)], ...
    ['mu=', num2str(mu-1), ', sigma=', num2str(sigma+2)], ...
    ['mu=', num2str(mu+6), ', sigma=', num2str(sigma+1)], ...
    })

Saturday 23 May 2015

Converting hexadecimal to binary

See also: Base conversion of positional number systems for other conversion functions.

Download
hex2bin2.m

Hexadecimal and binary
Hexidecimal and binary, like "normal" decimal are positional number systems, where individual digits have a value determined by their position in the sequence (see this post for a more detailed explanation). The difference between the three is that decimal has a base of 10 - it uses 10 numbers, 0-9, where hexadecimal has a base of 16 (using 0-9 and A, B, C, D, E and F) and binary has a base of 2 (using 0 and 1).

There are two ways to convert between hexadecimal and binary, mathematically, or by simply using a lookup table. Those interested in the mathematical process should check out the hex2dec2 and dec2bin2 functions (bin = dec2bin2(hex2dec2(hex))), which describe how to do each step. But if you'd just like to get it done and don't care about the maths behind it, just use hex2bin2. hex2bin2 uses the following lookup table to get the binary value of each hex digit.

Dec = Hex = Bin
 0   =   0   =   0000
 1   =   1   =   0001
 2   =   2   =   0010
 3   =   3   =   0011
 4   =   4   =   0100
 5   =   5   =   0101
 6   =   6   =   0110
 7   =   7   =   0111
 8   =   8   =   1000
 9   =   9   =   1001
10   =   A =   1010
11   =   B =   1011
12   =   C =   1100
13   =   D =   1101
14   =   E =   1110
15   =   F =   1111

Converting hexadecimal to decimal

See also: Base conversion of positional number systems for other conversion functions.

Download
hex2dec2.m

Hexadecimal numbers
"Normal" decimal numbers use a base 10 system, most likely because humans have 10 fingers. Hexidecimal is a base 16 number system (presumably because computer programmers have 16 fingers), so whereas the decimal number system has 10 numbers (0-9), hexadecimal has 16 (0-9, and A, B, C, D, E and F).

Like decimal (and binary) hexidecimal is a positional number system, where a numbers position determines its value. This means that calculating the value of a hexidecimal number is exactly the same process as calculating the value of a decimal number. Observe:

Consider the decimal value 2345. The value is obvious. It's 2345. But it can be broken down into the sum of 2000 + 300 + 40 + 5. And each of these values depends on the number of zeros, which is determined by position. Positions start from 0 and count up right to left, so the 2 in 2345 is in position 3, the 3 is in position 2, the 4 is in position 1 and the 5 in position 0. Mathematically this means the value of the 2 is 2*10^3 = 2000, of the 3 is 3*10^2 = 300, and so on. Overall the total value of the number is:

(2*10^3) + (3*10^2) + (4*10^1) + (5*10^0) = 2345.

For hexadecimal numbers, exactly the same process applies, except using a base of 16. The only caveat being that the letters, if there are any, need to be replaced with their equivalent decimal numbers first. So what's the value of the hex number 2345 (notice that for hex values without letters, it's not immediately obvious it's a hex number - so it should be written as either 0x2345 or 234516 to avoid this ambiguity).

0x2345 (hex) = (2*16^3) + (3*16^2) + (4*16^1) + (5*16^0) = 9026 (decimal).

If there are letters in the hex number, for example: 0xF34B, these need to be converted to decimal values first, which is simple: A=10, B=11, C=12, D=13, E=14, F=15. So,

0xF34B = (F*16^3) + (3*16^2) + (4*16^1) + (B*16^0) , or
0xF34B = (15*16^3) + (3*16^2) + (4*16^1) + (11*16^0) = 62283

This process is simple to implement in Matlab, and is very similar to the process to convert decimal values to binary (see dec2bin2). The only complication is the handling of the letters; in Matlab hexadecimal numbers must be strings, because they contain letters. This means that isn't not possible to directly perform mathematical operations on them, such as the above conversion, but Matlab has a number of convenient features for dealing with strings that we'll exploit.

Overall, this function will convert the hexadecimal input to decimal numbers, work out the value of each based on its position, the calculate the sum of all these values to give the final decimal value. It provides an alternative to Matlab's hex2dec function.

Friday 22 May 2015

Event / spike triggered average

Download
evTrigAvg.m

What caused an event?
Generally, event triggered averages are used to try and determine what caused an event to happen. They look at what preceded a number of events and take the average. In neuroscience events might be a spike recorded from a neuron in response to a certain stimulus. If the stimulus was white noise, for example, taking the time window from before the spike and averaging it would give the average stimulus that caused the neuron to fire. In finance the event could be a price change, and one might like to know if a consistent pattern occurs before similar price changes in order to react to them in the future.

evTrigAvg is designed to be general and agnostic to exactly what events and stimuli its dealing with are. All it needs is the "stimulus" and a logical vector of events (0s indicating no events, 1s indicating an event has occurred), and a window size to average over (in points).

Example
Let's generate some fake neural data as an example. Imagine an experiment where white noise is delivered to a subject and activity is recorded from a single neuron. The raw activity from the neuron will be noisy and will normally require preprocessing first, which will include filtering and event (spike) detection. Event detection is often done using a threshold of some value *the root mean square of the trace, where anything above this is marked as a spike. Or, if multiple units have been recorded, by much more complicated "spike sorting". I mention this mainly because it presents a fascinatingly complex problem, but it's not something we need to worry about here. The goal of preprocessing is to obtain a clean vector of events represented as 0s or 1s, we'll imagine the preprocessing has already been done, and we have this clean vector of events (spikes). We want to know what property of the stimulus caused each event.

nPoints = 10000;
stim = randn(1,nPoints);
resp = stim>2;
resp = [zeros(1,100), resp(1:end-100)];

nPoints determines the number of points in the stimulus (stim) and response (resp). stim is random noise drawn from a normal distribution. resp will be our vector of events (spikes), and for the sake of demonstration spikes are generated whenever the stim is greater than the entirely arbitrary value 2. Obviously, this means we know what stimulus property that is eliciting the spikes already, but pretend this is real data that we don't yet know anything about it. The line stim>2 returns a logical vector of 0s and 1s, which we'll also offset slightly relative to stim to emulate the latency that would exist in a real system (spikes aren't instant) by dumping 100 zeros at the start.

Let's plot out the first 1000 points of data we've just magically generated without having to do any pesky experiments.

subplot(2,1,1), plot(stim(1:1000))
line([1, 1000], [2, 2], 'LineStyle', '--', ...
    'color', 'k')
ylabel('Mag, arb'), title('Stimulus')
subplot(2,1,2), plot(resp(1:1000), 'r')
ylabel('Spikes'), title('Response')
xlabel('Time, pts')

Monday 4 May 2015

Alternative function: Net future value of a cash flow

See also the time value of money for other related Matlab functions.

Download
nfvFlow.m

Introduction
The future value (FV) of a single lump sum of cash is its present value (PV, its value right now) plus interest earned over a period of time. The future value of a cash flow is the sum of the FVs of the money received (or spent) at each time period. As each time periods go by, the amount of periods over which a subsequent income can potentially earn interest reduces.

For an introduction to future values and simple and compound interest, and a simple Matlab function for calculating the future value of a lump sum, see this post.

nfvFlow.m is an alternative to Matlab's fvvar function in the financial toolbox. nfvFlow includes a flag that allows interest payments to be made in advance or in arrears (intPaid=1 for advance, or =0 for arrears). Payments (payments) and periods (periods) can be explicitly specified, or entered as a single value if regular (see below for examples). The interest rate (rate) should be a decimal value, not a percentage. The outputs give the overall net future value of the cash flow (nfv) and the future value of each payment made at each time point (fvAtPeriod).

[nfv, fvAtPeriod]= nfvFlow(payments, rate, periods, intPaid)

For example, for a regular payment of £2000, over 4 periods with a rate of 5% and interest paid at the end of time periods (see first example below for details):

[nfv, fvAtPeriod] = nfvFlow(2000, 0.05, 4, 0).


Saturday 2 May 2015

Fitting a better psychometric curve (Wichmann and Hill 2001)

Code
GitHub: Matlab code and Python package

Introduction
Psychometric curves quantify the relationship between stimulus parameters and an observer's subjective experience. For an introduction to the concept of psychometric curves in signal analysis theory and a function and instructions to fit a basic curve in Matlab, see Introduction to psychometric curves and fitting a simple psychometric function. This article will focus on fitting a more complex curve with additional parameters that allow for the subjects fallibility, which improves estimation of the key parameter, discrimination sensitivity.

The psychometric function used here is from the paper The psychometric function:I. Fitting, sampling, and goodness of fit, F. Wichmann and N. Hill 2001. This paper, and its companion paper, provide detailed theory and methods for fitting a psychometric function, assessing its goodness of fit, and finding confidence intervals for the ranges of the parameters estimated. This article will demonstrate how to fit Wichmann and Hill's psychometric function in Matlab using the same data from a fictional 2AFC experiment in this post. This will allow an interested reader to make a direct comparison between the Wichmann and Hill curve and the approach using Matlab's glmfit function with a built in binomial distribution and a logit link function.

Sunday 19 April 2015

Introduction to psychometric curves and fitting a simple psychometric function

Code
GitHub: Matlab code and Python package

Introduction
An important aim of psychophysics is to quantify the relationship between stimulus parameters and an observer's subjective appreciation of what's going on. Signal detection theory approaches this problem by attributing perceptual sensitivity to thresholds that act at multiple levels in a system. In this context, a threshold could, for example, be how loud a sound needs to be to determine its location to a certain degree of accuracy, or how long a light needs to be on to determine its direction of movement. Above threshold, and the task is possible to a certain degree of accuracy. Below threshold and it's not possible to discriminate as accurately.

Experimentally, thresholds are often measured in two-alternative forced choice (2AFC) tasks or in go-no go paradigms (GNG). In 2AFC tasks a subject is asked to indicate one of two choices in response to a stimulus. For example, indicate white if a colour appears white, or black if it appears black in response to a shade of grey. Alternatively, GNG tasks require a subject to not respond ("no go") until a stimuli meets a certain criteria, then respond ("go").

In both of these types of task, a subjects performance can be measured as a function of the stimulus parameters - either as % correct, or similar, in 2AFC or d' in GNG, where d' is a function of hits, misses, and false alarms. Regardless of the exact metric used, the subjects performance can be described by two main parameters; bias (the "point of subject equality", PSE) and discrimination sensitivity (as in ability to discriminate between two conditions, such as white and black).

For example, imagine a subject performing a 2AFC task where they're presented with a shade of grey and they have to press one button to categorise the colour as white, or another button to categorise the colour as black. Intuitively, they're more likely to classify a grey stimulus as black the closer it is to black, and less likely the colour it is to white. So plotting their responses as a proportion of black responses (y) against the stimuli presented (x). We could use this sort of task to see how other factors, like the colour of a background, could affect perception of the shade of grey.



Wednesday 8 April 2015

Alternative function: Net present value of a cash flow

See also the time value of money for other related Matlab functions.

Download
npvFlow.m

Introduction
The net present value (NPV) of a cash flow is how much a flow of cash is worth right now. The "net" part refers to the fact that the cash flow isn't necessarily fixed, and may even be negative at certain points in time. Matlab has two functions in the financial toolbox for calculating NPV for fixed and variable cash flows (pvfix and pvvar respectively), npvFlow.m is designed to emulate the basic function of both these functions and works for fixed and variable cash flows.

For example, consider a cash flow of £200 per year for the next 4 years, with an annual interest rate of 4%. What is the value of this cash flow right now, at this point in time? It's not £800, it must be less. To work it out, we need to calculate the present value of each individual chunk of cash. The NPV is simply the sum of these.

This is done with the PV calculation (pvLump,m) of each lump sum, which is

PV = FV / (1+r)^n,

where r = interest rate and n = number of time periods.

So,
End of period 1: £200 received = PV1 = 200/(1+0.04)^1 = 192
End of period 2: £200 received = PV2 = 200/(1+0.04)^2 = 185
End of period 3: £200 received = PV3 = 200/(1+0.04)^3 = 178
End of period 4: £200 received = PV4 = 200/(1+0.04)^4 = 171

The sum of PV1-4 = NPV = 192 + 185 + 178 + 171 = £725.

Alternative function: Present value of lump sum

See also the time value of money for other related Matlab functions.

Download
pvLump.m

Introduction
The present value of a lump sum of cash is the value of cash available in the future, right now.

But why should the value of something be now compared to in the future? This is a core concept of what is known as the "time value of money", which basically states that money is worth more now than it is if received in the future. The reason for this is interest - if you have money now, you have the opportunity to earn interest on it. If you only get the money later, you miss this opportunity.

This concept also applies to cash flows, but is slightly more complex as the interest needs to be applied at multiple points in time - see npvFlow.m.

For example, for a lump sum, assuming a positive interest rate; £1000 in 2 years time is worth £1000 in 2 years, whereas £1000 now is worth £1000+2 years interest in 2 years time. But what is £1000 in 2 years time worth to us right now? This value is the present value (PV).

So, for a lump sum, the equation is a rearranged form of the future value (FV) compound interest equation (see fvLump.m). Intuitively, for compounding interest, the FV is the PV plus the interest from each time point:

FV = PV * (1+r)^n,

where r = interest rate and n = number of time periods.

This means to calculate the PV, the equation rearranges to:

PV = FV / (1+r)^n.

This is the equation we will implement in a simple Matlab function, pvLump.m.


Alternative function: Future value of lump sum

See also the time value of money for other related Matlab functions.

Download
fvLump.m

Introduction
The future value (FV) of a single lump sum of cash is its present value (PV, see also pvLump.m and npvflow.m) plus interest earned over a period of time. Interest can be earned in two ways - simple interest or compound. Simple interest is linear and is a proportion of the original amount applied at the end of the time period. Compound interest, which is mostly commonly used, increases in an exponential fashion as it is added at each time period and adds to the amount on which the interest is applied in the next period. fvLump provides an alternative to fvdisc in the financial toolbox.

Simple interest
FV = PV * (1+r*t)

Compounding interest
FV = PV * (1+r)^t

Where = interest rate and = number of time periods.

These equations are available in fvLump. For calculating the future value of a cash flow, for example, when saving a regular amount of money and earning interest, see nfvFlow.m.

Saturday 4 April 2015

Alternative function: Moving average


Download
movAv2.m

This is a slightly more advanced version of movAv and provides an alternative to movavg and tsmovavg ("time-series moving average"). This version allows weighting of the mean and setting of the lag value (rather than just centring the average automatically). It also shows examples of basic management of the variables in a functions workspace, using switch and case for convenient string logic, and how to perform a weighted mean calculation. See also movAv for a couple of examples when moving averages may or may not be appropriate.

Description
Compared to movAv.m, this version adds two features:

  • Lag - when performing a moving average with length n, n points in total are lost from the output. The lag value positions the output in a vector of the same length as in the input, by determining the number of NaNs at the start and end of the vector.
  • Weights - Included are a few random weights as examples in the script as examples, although the only one that might be useful is 'simExp', which exponentially weights the values in the centre of the average window higher than the outer points. Weights can also be supplied as a vector the same length as the average window (n). The absolute values of the weights are unimportant, as they're normalised to relative values that sum to 1 in the script.


Sunday 29 March 2015

Alternative function: Simple moving average

Download
movAv.m
(see also movAv2 - an updated version allowing weighting)

Description
Matlab includes functions called movavg and tsmovavg ("time-series moving average") in the Financial Toolbox, movAv is designed to replicate the basic functionality of these. The code here provides a nice example of managing indexes inside loops, which can be confusing to begin with. I've deliberately kept the code short and simple to keep this process clear.

movAv performs a simple moving average that can be used to recover noisy data in some situations. It works by taking an the mean of the input (y) over a sliding time window, the size of which is specified by n. The larger n is, the greater the amount of smoothing; the effect of n is relative to the length of the input vector y, and effectively (well, sort of) creates a lowpass frequency filter - see the examples and considerations section.

AdSense