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: , ,

Thursday, October 11, 2007

Painful (Elementary Level) Debugging

OMG.... I can't believe I made so many mistakes in such a simple script....

I was running data analysis in the library this afternoon, and need the last 2 digits of my 9-digit item number to recode some information (not the smartest item number scheme, I must admit). After I recoded all the variables necessary for the bias analysis, I then aggregated the raw data so that I could restructure the data format by subject for spss to run repeated measures ANOVA. When I opened the aggregated file, I found that for some subjects some of the recoded variables were lacking. This was when I found out that my item number had been messed up before they were imported to spss. If everything had worked correctly, there should be 8 possible values of the last two digits, and there should be equal number of cases for each possible values. However that was only true for my list 1 subjects.

Then I went back to inspect the Matlab script I used to extract the azk file. I tried everything I could imagine to save the mistake. Just to make a long story short, the problem (I think) was that I scanned the item numbers in the wrong number format. I used f32 to scan the 3 columns in the azk output. However, whereas f32 is good for scanning RT and COT values, it's not ideal for scanning item numbers. This is because the item number is always an integer, scanning them as floating point numbers would waste the precision on the none existent decimals.

In the end I just changed one small thing and the problem was saved. I feel really dumb because I actually used the correct format to scan the data in an earlier version of the script, but somehow I changed the format for some unknown reason afterwards, and created all these troubles for myself. The only thing that's conforting is that, I have learned my lesson, and I can confidently say that I know a bit better today and yesterday. I guess that's the most important thing :)

Labels: ,

Tuesday, October 09, 2007

textscan and textread

Last week I was trying to demonstrate to my RA Jenn how I used the Matlab script to extract data from DMDX azk files and pull the data from all lists into onecomplete file. I tried to demonstrate on the computer in Park 320, so that she will be able to do the same if I give her similar assignments.

However I forgot one important thing - the Matlab installed on computers in 320 is 6.5, whereas I composed the script in the Matlab 7 on my laptop. The main command I used in the script - textcan - does not come into existance until Matlab 7. The comparable command to use in Matlab 6.5 is textread, which has very similar argument structure to textscan. The main differece is the format of the data read in, and therefore how the data are referred to in the following scriptfor any manipulation. One main problem I have right now is that I cannot utilize the participant ID information from the read in matrix. I thought the format of this cell would be the same as when it's read in by textscan (string). But it doesn't seems to work this way. I'm puzzled exceedingly...

No wonder it didn't work when I demo the process in PC.... I swithced the demo to my laptop immediately, but it wasn't util several days later that I realized where the problem had came from.
I am hoping that other members in the lab can also benefit from the scripts I created, so I'm still trying to modify the scripts for Matlab 6.5. However, judging from the number of other random tasks that constantly fall on me, I serious doubt when I will have a big chunk of time to play with my scripts....

Labels: ,

Sunday, September 30, 2007

My first m file script :)

Currently the NCP lab (human part) mainly uses DMDX to run standard cognitive psychology experiments. For those people who are interested in knowing further about DMDX, please refer to this site:

http://www.u.arizona.edu/%7Ejforster/dmdx.htm

My goal here is very simple: to make my life easier and hopefully graduate sooner than later. Today I just wrote my first m file so that I can directly extract the ID, Item Number, RT and COT information from the DMDX output .azk file. Since I'm new to Matlab programing, the script might not be written in the most elegant way to handle such tasks. However I consider this a good start (for myself) to learn programming better and make something useflul out of my time :)

The script turns the default azk output into a four column array saved in .txt format (tab delimited)

Before the conversion, the default azk output looks like this:

===================================

Subjects incorporated to date: 005
Data file started on machine PARK375NCP1

**********************************************************************
Subject 1, 09/18/2007 17:54:23 on PARK375NCP1, refresh 16.66ms, ID 1001
Item RT COT
10111612 3504.07 0.00
10111713 464.16 6681.79
10111522 -693.13 11897.25
10111722 786.34 17112.71
........

===================================

After the conversion, the txt file looks like this:

===================================

1001 10111612 3504.07 0.00
1001 10111713 464.16 6681.79
1001 10111522 -693.13 11897.25
1001 10111722 786.34 17112.71
........

===================================

This format (hopefully) is a lot easier to process in other programs such as Excel or SPSS (what I use currently).

The file is called "azk2txt_c.m". For novice Matlab users like myself, here's how I use the script:

(1). Place the file in a folder that's within your Matlab paths
(2). The azk file(s) to be converted should be in the same folder as azk2txt_c.m
(3). In the Matlab command line, type in azk2txt_c and hit enter (or open the m file and run the script)
(4). At prompt, enter the name of the azk file (without .azk)
(5). At prompt, enter the trail number for each subject (don't know how to get around this yet.... shouldn't be too difficult to make this step automatic. But as I said, I have very limited programing ability....)
(6). A extracted txt file will be automatically generated in your working folder. The default name for the txt file is azk_[the original filename].txt

Please download the file on the following page:

http://estellaliu.googlepages.com/resource

Labels: , ,