Wednesday, April 25, 2012

Peak detection in matlab


A great function for peak detection in matlab is the peakfinder (downloadable at http://www.mathworks.com/matlabcentral/fileexchange/25500). Here's a set of super basic instructions regarding how to use this function (for people with little experience with matlab):

Setting up peakfinder

  1. Download the peakfinder package from the link above. extract/unzip the folder and put it in a folder under your matlab path
  2. In matlab, add the peakfinder folder to path (file -> set path, click on set path, select the peakfinder folder by browsing, click on save, then click on close)


Using peak finder
Assuming your signal is already in the matlab workspace called x, to find the peaks using peakfinder, type in the following in the matlab command window:

[peaks, locs] = findpeaks(x)

This would result in two vectors:

peaks = [# # # # # ... ] <-- indicates all the peaks that are found in x
locs = [# # # # # ...] <-- indicates all the locations/positions of the found peaks

To examine the peaks found using this setting, you can type in the following:

plot(x);
hold on;
plot(locs,peaks,'o','MarkerEdgeColor','r')

More functions/setting can be consulted through typing 'help peakfinder' in the matlab command window.

Labels: , ,

Tuesday, October 16, 2007

SDT Analysis in SPSS

This evening I was trying to figure out how to run signal detection analysis (SDT) using SPSS, rather than having to caculate SDT parameters (e.g., d') in Excel first and then do the statistical analysis in SPSS. Not that I have any concern about the accuracy of the output from Excel. I just don't trust myself to be able to calculate everything manually in Excel and not making a single mistake the entire time (even if I can do it once in a while, I don't think I can finish the process without any mistake everytime). I decided that if I can figure out how to calculate the SDT parameters in SPSS, it would greatly improve the accuracy of the analysis, and reduce the time needed to conduct the analysis and double check the accuracy.

The calculation of d' is pretty straight forward in theory. It's given by the distance between the center of the hypothesized distributions of signal and no signal (noise), divided by the spread of the distributions. This could then be further translated into this formula: d' = z(hit) - z(false alarm).

The main problem I encountered was that even though I know how to calculate the inversed culmulative normal distribution in in Excel (given by the function NORMSINV), I don't know how to do the same thing in SPSS. I tried google "sdt analysis in spss," "how to calculate d prime in spss," ... etc but didn't really find what I needed. Then I googled "NORMSINV equivalent in spss" and I came across this following document:

http://www.stat.auckland.ac.nz/~leila/07S2_SPSS_Excel_Workshop_II.pdf

Luckily this document is exactly what I was looking for: it introduces how to calculate culmulative normal districution functions in SPSS AND in Excel. To calculate d' and other SDT parameters in SPSS, you can use the following syntax (these are specifically for 2AFC tasks):

===================================
COMPUTE dprime = (IDF.NORMAL(HitRate,0,1) - IDF.NORMAL(FArate,0,1))/SQRT(2) .
EXECUTE .
COMPUTE c = -.5*(IDF.NORMAL(HitRate,0,1) + IDF.NORMAL(FArate,0,1)) .
EXECUTE .
COMPUTE cprime = c/(IDF.NORMAL(HitRate,0,1) - IDF.NORMAL(FArate,0,1)) .
EXECUTE .
===================================

In the context of a 2AFC task (let's say stimulus A and stimulus B), HitRate refers to the probablity of responding A when A is presented, and FArate refers to the probability of responding A when B is presented.

Because I started with the trial by trial data in the analysis, I had to aggregate and restructure the data several times in order to calculate the SDT parameters and then run the repeated measures ANOVA. However, once the syntax file is set up, if I want to run the same analysis again, all I need to do is change the file names that get retrieved and saved in the syntax, and then click on the run all icon. Then the entire analysis could be done within seconds :)

Labels: ,