Skip to content

Instantly share code, notes, and snippets.

@ferryzhou
ferryzhou / alsa_loopback_min.c
Created February 22, 2012 20:18
A Minimal ALSA Loopback Program
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define BUF_BYTES 128
int main (int argc, char *argv[]) {
int err;
unsigned char buf[BUF_BYTES];
@ferryzhou
ferryzhou / get_segments.m
Created March 31, 2012 21:30
Get segments from a binary vector
function segs = get_segments(o)
segs = [];
if o(1), st = 1; state = 1;
else state = 2; end
for i = 2 : length(o)
if o(i)
if state == 2
@ferryzhou
ferryzhou / mkdir_if_not_exist.m
Created March 31, 2012 22:59
matlab mkdir if not exist
function mkdir_if_not_exist(dirpath)
if dirpath(end) ~= '/', dirpath = [dirpath '/']; end
if (exist(dirpath, 'dir') == 0), mkdir(dirpath); end
end
@ferryzhou
ferryzhou / align.m
Created April 16, 2012 14:50
Align two signals using correlation
function [ao, bo, offset, y] = align(a, b)
% align two signals a and b
% ao and bo are aligned ones with same length
% y is the correlation xcorr(a, b)
% a_ind + offset -> b_ind
% if offset >= 0, cut a, else cut b
an = length(a); bn = length(b);
N = max(an, bn);
@ferryzhou
ferryzhou / get_threshold.m
Created April 16, 2012 18:19
Automatically generate a threshold for a vector. Can be used to filter out noise.
function t = get_threshold(v)
% pitch the lowest 20%
[m, inds] = sort(v);
P = fix(0.2 * length(v));
nt = m(P);
P = fix(0.8 * length(v));
nt2 = m(P);
@ferryzhou
ferryzhou / init_option.m
Created May 3, 2012 14:37
Initialize variable based on options from input
function val = init_option(options, name, default_val)
% usage lamda = init_option(options, 'lamda', 0.01);
if isfield(options, name), val = options.(name); else val = default_val; end
end
@ferryzhou
ferryzhou / get_local_maxima.m
Created December 18, 2012 21:21
get maxima of a gray scale image
function [I, J, xvals] = get_local_maxima(imcor, radius)
% a = rand(10);
% [I, J, xvals] = get_local_maxima(a, 3);
% figure, imagesc(a); hold on; scatter(J, I);
[m, n] = size(imcor);
nim = ones(m + radius * 2, n + radius * 2) * 1e-10;
nim(radius + (1:m), radius + (1:n)) = imcor;
cols = im2col(nim, [2*radius + 1, 2 * radius + 1], 'sliding');
@ferryzhou
ferryzhou / nextpow2.c
Created January 29, 2013 15:41
next power of 2
int nextpow2 (int x) {
if (x < 0)
return 0;
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x+1;
@ferryzhou
ferryzhou / concatenate_binary_files.rb
Created February 7, 2013 20:46
concatenate binary files with ruby
# given a directory or a set of files
# concatenate the binary files
# output a single binary file
# usage:
# ruby concatenate_binary_files "/xxx/xx/*.bin" "/xxx/xx.bin"
infiles = ARGV[0]
outpath = ARGV[1]
File.open(outpath, 'wb') do |outfile|
@ferryzhou
ferryzhou / mex_show_sparse.cpp
Created March 19, 2013 21:10
show sparse matrix in mex file
#include "mex.h"
// undef needed for LCC compiler
#undef EXTERN_C
// mex mex_show_sparse.cpp -O -largeArrayDims
// a = sparse(rand(10)<0.1)
// mex_show_sparse(a);
// b = a .* rand(10)
// mex_show_sparse(b);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {