MATLAB环境下4种噪声生成

发布于:2024-07-05 ⋅ 阅读:(18) ⋅ 点赞:(0)

生成噪声包括:

1)粉红色(闪烁)噪声-功率谱密度斜率-3 dB/oct。, - 10db /dec

2)红色(布朗)噪声-功率谱密度斜率-6 dB/oct。, - 20db /dec

3)蓝色噪声-功率谱密度斜率+3 dB/oct。, + 10db /dec

4)紫色(紫色)噪声-功率谱密度斜率+ 6db /oct。, +20 dB/dec

clear, clc, close all

% signal parameters
fs = 44100;         % sampling frequency, Hz
T = 60;             % signal duration, s
N = round(fs*T);    % number of samples

% noise generation
x = rednoise(1, N); % red noise, PSD falls off by -20 dB/dec

% calculate the noise PSD
winlen = 2*fs;
window = hanning(winlen, 'periodic');
noverlap = winlen/2;
nfft = winlen;

[Pxx, f] = pwelch(x, window, noverlap, nfft, fs, 'onesided');
PxxdB = 10*log10(Pxx);

% plot the noise PSD
figure(1)
semilogx(f, PxxdB, 'r', 'LineWidth', 1.5)
grid on
xlim([1 max(f)])
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14)
xlabel('Frequency, Hz')
ylabel('Magnitude, dBV^{2}/Hz')
title('Power Spectral Density of the Noise Signal')

clear, clc, close all

% signal parameters
fs = 44100;         % sampling frequency, Hz
T = 0.1;            % signal duration, s
N = round(fs*T);    % number of samples
t = (0:N-1)/fs;     % time vector

% signal generation
s = 10*sin(2*pi*1000*t + pi/6);

% noise generation
SNR = 20;                       % define SNR in dB
Ps = 10*log10(std(s).^2);       % signal power, dBV^2
Pn = Ps - SNR;                  % noise power, dBV^2
Pn = 10^(Pn/10);                % noise power, V^2
sigma = sqrt(Pn);               % noise RMS, V

n = sigma*pinknoise(1, N);      % pink noise generation
x = s + n;                      % signal + noise mixture

% plot the signal
figure(1)
plot(t, x, 'r', 'LineWidth', 1.5)
grid on
xlim([0 max(t)])
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14)
xlabel('Time, s')
ylabel('Amplitude')
title('Sine wave signal + Pink noise in the time domain')

% check the SNR
commandwindow
disp(['The target SNR is ' num2str(SNR) ' dB.' newline ...
      'The actual SNR is ' num2str(snr(x, n)) ' dB.'])


clear, clc, close all

% signal parameters
fs = 44100;     % sampling frequency, Hz         
m = 256;        % number of the rows of the noise matrix
n = 256;        % number of the columns of the noise matrix

% noise generation
xred = rednoise(m, n); 
xpink = pinknoise(m, n); 
xblue = bluenoise(m, n); 
xviolet = violetnoise(m, n); 

% visual presentation
figure(1)
colormap gray
subplot(2, 2, 1)
imagesc(xred)
title('Red noise')
subplot(2, 2, 2)
imagesc(xpink)
title('Pink noise')
subplot(2, 2, 3)
imagesc(xblue)
title('Blue noise')
subplot(2, 2, 4)
imagesc(xviolet)
title('Violet noise')

代码由知乎学术咨询获得:
https://www.zhihu.com/consult/people/792359672131756032?isMe=1

工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。


网站公告

今日签到

点亮在社区的每一天
去签到