Skip to content

Instantly share code, notes, and snippets.

@smithdanielle
smithdanielle / gaussFilter1D.m
Last active April 29, 2020 19:47
Create a 1-dimensional gaussian filter and apply it (MATLAB)
function gaussFiltered = gaussFilter1D(m, sigma)
cutoff = ceil(3*sigma);
h = fspecial('gaussian',[1,2*cutoff+1],sigma); % 1D filter
gaussFiltered = conv2(h,h,m,'same');
@smithdanielle
smithdanielle / normaliseXY.m
Last active August 29, 2015 14:09
MATLAB function for normalising an image matrix between [X;Y]
function normalised = normaliseXY(array, x, y)
% Normalize to [0, 1]:
m = min(array(:));
range = max(array(:)) - m;
array = (array - m) ./ range;
% Then scale to [x,y]:
rangeXY = y - x;
normalised = (array*rangeXY) + x;
@smithdanielle
smithdanielle / jitter.m
Created October 8, 2014 14:00
Add small amount of noise to vector, matrix or N-D array in MATLAB.
function y = jitter(x, factor, uniformOrGaussianFlag, smallOrRangeFlag, realOrImaginaryFlag)
% Adds a small amount of noise to an input vector, matrix or N-D array. The
% noise can be uniformly or normally distributed, and can have a magnitude
% based upon the range of values of X, or based upon the smallest
% difference between values of X (excluding 'fuzz').
%
% NOTE: This function accepts complex values for the first input, X. If
% any values of X have imaginary components (even zero-valued imaginary
% components), then by default the noise will be imaginary. Otherwise, the
% default is for real noise. You can choose between real and imaginary
@smithdanielle
smithdanielle / hextile.m
Created October 8, 2014 13:43
A MATLAB script to generate hexmaps
function [h,xy]=hextile(rect,size)
% hextile(rect,size)
% rect - [x y w h]
% size - distance between hexes (default 1)
% returns
% h - plot handle
% xy - centers of all hexagons drawn
[x,y,w,h] = deal(rect(1),rect(2),rect(3),rect(4));
if nargin < 2, size = 1; end
[c,s]=deal(cos(pi/6),sin(pi/6));
### Please note, this script requires answers to be coded as "A" for agree, or "D" for disagree ###
AQ[,1:50]<-as.character(unlist(AQ[,1:50])) # recode to character
AQAnswers<-c('D','A','D','A','A','A','A','D','A','D','D','A','A','D','D','A','D','A','A','A','A','A','A','D','D','A','D','D','D','D','D','D','A','D','A','D','D','D','A','D','A','A','A','D','A','A','D','D','D','D') # create key for AQ
AQ<-data.frame(lapply(AQ, as.character), stringsAsFactors=FALSE) # convert df AQ to characters to enable matching by grepl
AQ<-as.data.frame(mapply(grepl,AQAnswers,AQ))*1 # calculate AQ score
# Subscales
AQ$AQ.socialSkill<-apply(AQ[,c(1,11,13,15,22,36,44,47:48)],1,sum)
AQ$AQ.attentionSwitching<-apply(AQ[,c(2,4,10,16,25,32,34,37,43,46)],1,sum)
@smithdanielle
smithdanielle / failedStereoSync.txt
Created April 24, 2014 10:22
Failure to sync Nvidia glasses using Datapixx and PTB3
>> crossedBaselineRT
PTB-INFO: Display ':0.0' : X-Screen 0 : Assigning primary output as 2 with RandR-CRTC 0 and GPU-CRTC 0.
put in name test
PTB-INFO: This is Psychtoolbox-3 for GNU/Linux X11, under Matlab 64-Bit (Version 3.0.11 - Build date: Dec 29 2013).
PTB-INFO: Type 'PsychtoolboxVersion' for more detailed version information.
PTB-INFO: Most parts of the Psychtoolbox distribution are licensed to you under terms of the MIT License, with
PTB-INFO: some restrictions. See file 'License.txt' in the Psychtoolbox root folder for the exact licensing conditions.
@smithdanielle
smithdanielle / corstars.R
Created April 17, 2014 16:34
A function to output a correlation matrix with p-values and significance indicators in `R`.
corstars <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", " |"))
R <- format(round(cbind(rep(-1.111, ncol(x)), R), 3))[,-1]
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
diag(Rnew) <- paste(diag(R), " |", sep="")
rownames(Rnew) <- colnames(x)
@smithdanielle
smithdanielle / check.packages.r
Created April 1, 2014 13:23
Check if multiple R packages are installed. Install them if they are not,then load them into the R session.
# check.packages function: install and load multiple R packages.
# Check to see if packages are installed. Install them if they are not, then load them into the R session.
check.packages <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
# Usage example