Skip to content

Instantly share code, notes, and snippets.

@qiuwch
qiuwch / randPerm.c
Last active November 28, 2023 12:21
Random permutation in C, equivalent to randperm in MATLAB, #tags: algorithm
int perm[SZ];
for (int i = 0; i < SZ; i++) perm[i] = i;
// Random permutation the order
for (int i = 0; i < SZ; i++) {
int j, t;
j = rand() % (SZ-i) + i;
t = perm[j]; perm[j] = perm[i]; perm[i] = t; // Swap i and j
}
@qiuwch
qiuwch / randperm.c
Last active March 31, 2017 02:46
Sample a value from a distribution #tags: algorithm
double random_num = (double)rand() / RAND_MAX;
for (k = 0; k <= MAX_INTENSITY; k++)
{
if (random_num < probs[k])
{
synthesized[i*im_width+j] = k; // update synthesized image
break;
}
else
random_num = random_num - probs[k];
@qiuwch
qiuwch / simpleMatrix.h
Created May 10, 2015 03:40
A simple matrix implementation, useful for course project
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <complex> // std::complex, std::abs
#define CHECK_ACC
#define EPS 10e-3 // a very small value
// Float matrix
template<typename T>
@qiuwch
qiuwch / figureTips.m
Last active August 29, 2015 14:20
Set the figure size of MATLAB
set(gcf, 'PaperPosition', [0 0 20 10]);
% PaperPosition is used for exporting figure
% PaperUnits: default inches for U.S., cm for Asia and Euro. normalized is the best choice
set(gcf, 'Position', [0 0 20 10]);
% Position is used for GUI position
% Default unit is pixels
% Units: Default pixel
xTickLabel = arrayfun(@(x) num2str(x), betas, 'UniformOutput', false);
@qiuwch
qiuwch / todo.py
Created May 12, 2015 02:34
Utility python script.
#!/usr/bin/env python
import argcomplete, argparse, requests, pprint
import types
import os, sys, pprint
# http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory
cwd = os.getcwd()
print cwd
sys.path.append(cwd)
import todo
@qiuwch
qiuwch / GoProRename.py
Created May 12, 2015 03:05
Batch rename and convert time lapse of GoPro.
import glob
# Update this script to make it clear enough to merge two folders
# This is done manually right now
pattern = '108GOPRO/G012*.JPG'
srcdir = pattern.split('/')[0]
prefix = pattern.split('/')[1].split('*')[0]
tgtdir = srcdir + '/' + prefix
frames = glob.glob(pattern)
print 'Srcdir:', ddir, 'Prefix:', prefix, 'Tgtdir:', tgtdir
@qiuwch
qiuwch / iminfo.sh
Last active March 31, 2017 02:46
Batch resize images #tags: tips
identify *.png
@qiuwch
qiuwch / humanMocap.py
Created May 22, 2015 22:20
Blender snippets
import bpy
from bpy import data as D
from bpy import context as C
from mathutils import *
@qiuwch
qiuwch / human.py
Created May 22, 2015 22:48
Snippets to control human model of makehuman
##
bpy.data.objects
py.data.objects.keys()
# List all objects
# ['Camera', 'CustomShapes', 'GZM_Ball025', 'GZM_Breast_L', 'GZM_Breast_R', 'GZM_Circle025', 'GZM_CircleChest', 'GZM_CircleHips', 'GZM_CircleSpine', 'GZM_CrownHips', 'GZM_Cube025', 'GZM_FaceHead', 'GZM_FaceJaw', 'GZM_Foot', 'GZM_FootIK', 'GZM_Gaze', 'GZM_Hand', 'GZM_HandIK', 'GZM_Head', 'GZM_Jaw', 'GZM_Knuckle', 'GZM_LoLid', 'GZM_Master', 'GZM_Neck', 'GZM_RevFoot', 'GZM_RevToe', 'GZM_Root', 'GZM_Shoulder', 'GZM_Toe', 'GZM_Tongue', 'GZM_UpLid', 'Human', 'Human:Body', 'Human:HighPolyEyes', 'Human:jeans01', 'Human:mhair02', 'Human:Shirt01', 'Human:tongue01', 'Sun']
human = bpy.data.objects['Human']
light = bpy.data.objects['Sun']
human.pose
@qiuwch
qiuwch / s3upload.py
Created June 5, 2015 20:38
Script for Amazon S3 upload
#!env python
# script to sync publish this folder to a web server
import time
datetimeLog = {}
def loadTimestamp():
# parse stored log
import os
timeFormat = '%Y-%m-%dT%H:%M:%SZ'
logFilename = '.log'