简体   繁体   中英

Same noisy signal, Matlab vs Python

Im trying to get a similar signal from a code in Matlab with python.

Code in MATLAB:

Fs = 1e3;               % sampling frequency (in Hz)
L = 1e5;                % signal length (number of samples)
f0 = 0.1*Fs;            % cycle frequency (in Hz)   

x = rand(L, 1);
a = [1 -2*cos(2*pi*.2)*.9 .9^2];
x = filter(1,a,x);
x = x.*(1 + sin(2*pi*(0:L-1)'*f0/Fs));
x = x +std(x)*rand(L,1);
histogram(x,100);

Code in Python:

Fs=10**3
L=10**5
f0=0.1*Fs
x=np.random.normal(0,1,L)
a=[1,-2*np.cos(2*np.pi*.2)*.9,.9**2]
x=sps.lfilter([1],a,x)
Random_modulated_signal=x*(1+np.sin(2*np.pi*np.arange(0,L)*f0/Fs))
Rms_Whitenoise=x+np.std(x)*np.random.normal(0,1,L)
plt.hist(Rms_Whitenoise,bins=100,edgecolor='k')
plt.show()

If I plot a histogram of both signals, they dont share the same behaviour. The signals differ when I add std(x)*rnd(L,1) .

What is strange is that I used the same codes as shown before, but instead of using a normal distribution, I used rand('twister', 2) and np.random.seed(2) for generating same values in both programs and it worked.

Please can someone clarify what am I am doing wrong?

Histogram of both signals 两个信号的直方图

Matlab's rand is a uniform random distribution, not a normal distribution. The equivalent of numpy.random.normal in matlab is randn . The numpy equivalent of matlab's rand is numpy.random.random .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM