Skip to content

Instantly share code, notes, and snippets.

View z0gSh1u's full-sized avatar

ZHUO Xu z0gSh1u

View GitHub Profile
@z0gSh1u
z0gSh1u / expand.ts
Created May 8, 2023 09:28
Full expand TypeScript type
// https://stackoverflow.com/questions/57683303/how-can-i-see-the-full-expanded-contract-of-a-typescript-type
// expands object types one level deep
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
// expands object types recursively
type ExpandRecursively<T> = T extends object
? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
: T;
@z0gSh1u
z0gSh1u / cudaCheckError.c
Created October 26, 2022 09:06 — forked from jefflarkin/cudaCheckError.c
A simple macro for checking errors after CUDA library calls.
//Macro for checking cuda errors following a cuda launch or api call
#define cudaCheckError() { \
cudaError_t e=cudaGetLastError(); \
if(e!=cudaSuccess) { \
printf("Cuda failure %s:%d: '%s'\n",__FILE__,__LINE__,cudaGetErrorString(e)); \
exit(0); \
} \
}
@z0gSh1u
z0gSh1u / reflection_import.py
Created November 10, 2021 14:48
`importlib` reflection import.
readerClazz = importlib.import_module(
'reader.{}'.format(selectedReader)) # .py file level
readerClazz = eval('readerClazz.{}'.format(selectedReader))
@z0gSh1u
z0gSh1u / ssh_nopwd.sh
Created October 4, 2021 13:08
Use key pair to SSH connect remote server.
ssh-keygen -t rsa # if needed
ssh-copy-id -i ~/.ssh/id_rsa.pub username@server_ip
@z0gSh1u
z0gSh1u / show_imgs.py
Created July 16, 2021 06:19
Show images faster using matplotlib.
def show_imgs(imgs, rows, cols=None, cmap='gray'):
if rows == 1:
cols = len(imgs)
elif not cols:
raise BaseException("Cannot infer `cols`.")
plt.figure(); plt.tight_layout()
code_base = int('{}{}1'.format(rows, cols))
for i in range(len(imgs)):
plt.subplot(code_base); plt.imshow(imgs[i], cmap=cmap)
code_base += 1
@z0gSh1u
z0gSh1u / equalizeHist.cpp
Created July 6, 2021 01:48
Zero-dep `equalizeHist` implementation for 8-bit grayscale image
// 直方图均衡化
void equalizeHist(uint8 *img, int h, int w) {
// 计算各像素值的出现次数
float *freq = new float[256];
memset(freq, 0, 256 * sizeof(float));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
short grayValue = img[i * h + j];
freq[grayValue] += 1;
}
@z0gSh1u
z0gSh1u / find_longest_all_true.py
Last active July 5, 2021 08:44
Find longest continous sequence of `True` and its start
def _find_longest_all_true(arr):
curlen = 0; maxlen = 0; p = 0; start_index = 0
while p < len(arr):
if arr[p] == True:
curlen += 1
elif curlen > maxlen:
maxlen = curlen
curlen = 0
start_index = p - maxlen
p += 1
@z0gSh1u
z0gSh1u / fir.py
Created May 23, 2021 15:30
FIR Filter
# coding: utf-8
# 《数字信号处理》课程实验
# FIR数字滤波器设计
# 09017227 卓旭
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False
@z0gSh1u
z0gSh1u / fft_demo.py
Created May 23, 2021 15:12
Test for FFT
# coding: utf-8
# 《数字信号处理》课程实验
# FFT与IFFT实测应用(作图)
# 09017227 卓旭
import matplotlib.pyplot as plt
import numpy as np
from fft import *
@z0gSh1u
z0gSh1u / fft.py
Created May 23, 2021 15:12
Fast Fourier Transform (1D)
# coding: utf-8
# 《数字信号处理》课程实验
# FFT与IFFT实现(一维)
# 09017227 卓旭
import math
'''
自定义复数类