gausssian sampling

import sys
import os
import cv2
import math
import numpy as np
import random as rdm

def gen_gaussian_sample(n):
    data = np.zeros([n*2,1])
    for k in range(n):
        u1 = rdm.random()
        u2 = rdm.random()
        r = math.sqrt(-2*math.log(u1))
        data[k] = r * math.cos(2*3.14*u2)
        data[k + n] = r * math.sin(2*3.14*u2)
    return(data)


def do_stat(data):
    N = data.size
    s = data.sum()
    d2 = data * data
    s2 = d2.sum()
    mean = s / N
    var = s2 / N - mean ** 2
    return((mean,var))

if __name__ == "__main__":
    data = gen_gaussian_sample(10000)
    (mean,var) = do_stat(data)
    
    print mean
    print var

    
    radius = 510 / 3
    width = (radius * 3) * 2 + 1 #3-sigma
    height = 100
    num = data.size
    
    h = np.zeros([width,1])
    
    for k in range(data.size):
        d = int(data[k] * radius + width/2)
        if d < 0:
            d = 0
        if d > width - 1:
            d = width - 1
        h[d] = h[d] + 1

    with open("h.txt", "w") as file:
        for k in h:
            s = str(k) + "\n"
            file.write(s)
            

    norm = h.max()    
    img = np.zeros([height, width], np.uint8)
    for col in range(width):
        row = h[col] * (height - 1) / norm
        row = int(height - 1 - row)
        img[row,col] = 255
        
    cv2.imshow("bmt", img)
    cv2.waitKey(-1)
        
        
            
        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 以下是用Matlab编写的多尺度能量熵代码: ```matlab function [ H, sc ] = multiscale_energy_entropy( x, scale ) %MULTISCALE_ENERGY_ENTROPY Computes the multiscale energy entropy % [ H, sc ] = multiscale_energy_entropy( x, scale ) % % INPUT % x: 1D signal. % scale: vector with the scales to compute energy entropy. % % OUTPUT % H: entropy for each scale. % sc: scaled signal (cell array). % % REFERENCE % C. W. Wu, K. Y. Chan, and Y. H. Qu, "Multiscale entropy analysis of % physiological signals," in Encyclopedia of Biomedical Engineering, % Metin Akay, Ed. Springer US, 2018, pp. 1–13. % % EXAMPLE % x = randn(1000,1); % scale = 1:5; % [H,sc] = multiscale_energy_entropy(x,scale); % % See also ENERGY_ENTROPY. n = length(x); m = length(scale); sc = cell(1,m); for i=1:m sc{i} = rescale( smooth(x,scale(i)) ); end H = zeros(m,1); for i=1:m H(i) = energy_entropy(sc{i}); end end function [ H ] = energy_entropy( x ) %ENERGY_ENTROPY Computes the energy entropy of a signal. % H = energy_entropy( x ) % % INPUT % x: 1D signal. % % OUTPUT % H: energy entropy. % % REFERENCE % C. W. Wu, K. Y. Chan, and Y. H. Qu, "Multiscale entropy analysis of % physiological signals," in Encyclopedia of Biomedical Engineering, % Metin Akay, Ed. Springer US, 2018, pp. 1–13. % % EXAMPLES % x = randn(1000,1); % H = energy_entropy(x); % % See also SVD_ENTROPY. N = length(x); L = N/2; x = x(1:2*L); [x_d,~] = integer_powers_of_two_decomposition(x); E = zeros(length(x_d),1); for i=1:length(x_d) E(i) = sum(abs(x_d{i}).^2); end E = E / sum(abs(x).^2); H = -sum(E .* log2(E)); end function [ x_d, varargout ] = integer_powers_of_two_decomposition( x ) %INTEGER_POWERS_OF_TWO_DECOMPOSITION Decomposes a signal into integer %powers of two signals. % [ xd, decvars] = integer_powers_of_two_decomposition( x ) % % INPUT % x: 1D signal. % % OUTPUT % xd: cell array with integer powers of two signals. % decvars:variances of the integer powers of two signal. % % EXAMPLES % x = randn(1000,1); % [xd,dvar] = integer_powers_of_two_decomposition(x); % plot( (0:length(x)-1)/length(x), abs(x), 'k',... % (0:length(xd{1})-1)/length(x), abs(xd{1}*sqrt(dvar(1))), 'b',... % (0:length(xd{2})-1)/length(x), abs(xd{2}*sqrt(dvar(2))), 'r') N = length(x); L = N/2; x_f = fft(x)/N; x_f(N/2+2:end) = 0; x_f(2:N/2) = 2 * x_f(2:N/2); % scaling x = ifft(x_f)*N; ind = cell(1,log2(N/2)); ind{1} = [(0:L-1)*2+1, (1:L)*2]; for k=2:log2(N/2) ind{k} = [(0:L-1)*2^(k-1)+1, (1:L)*2^(k-1)]; end x_d = cell(1,log2(N/2)); for k=1:log2(N/2) xd = zeros(size(x)); xd(ind{k}) = x(ind{k}); x_d{k} = xd; end if nargout>1 varargout{1} = zeros(length(x_d),1); for i=1:length(x_d) varargout{1}(i) = var(x_d{i}); end end end function [ y ]=smooth(x, w) %SMOOTH Signal smoothing by means of convolution with the Gausssian window. % y=smooth(x, w) % % INPUT % x: vector with the signal to smooth. % w: width of the window (in samples). % % OUTPUT % y: smoothed signal. % % EXAMPLE % x = randn(1000,1); % y = smooth(x,50); % plot(x,'k',y,'r') w = round(w); if mod(w,2)==0 w = w+1; end gauss = exp(-( (-w+1:1:w-1)/(2*(0.1*w)) ).^2); y = conv(x, gauss, 'same'); y(1:w) = y(w+1); y(end-w+1:end) = y(end-w); end function [ y ] = rescale( x ) %RESCALE Rescales a signal to be between 0 and 1. % y = RESCALE(x) % % INPUT % x: input signal. % % OUTPUT % y: rescaled signal (between 0 and 1). % % EXAMPLE % x = randn(1000,1); % y = rescale(x); % plot(x,'k',y,'r') y = (x-min(x))/(max(x)-min(x)); end ``` 该代码实现了多尺度能量熵的计算,其`multiscale_energy_entropy`对信号进行了多尺度平滑处理,并计算了每个尺度的能量熵;`energy_entropy`计算了信号的能量熵;`integer_powers_of_two_decomposition`将信号分解为2的幂次信号,`rescale`将信号缩放到0~1之间。 您可以将上述代码保存为.m文件,然后在Matlab运行该文件,例如 [H,sc] = multiscale_energy_entropy(x,1:5),其x为待分析的信号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值