function [Y,NOISE] = noisegen(X,SNR)% noisegen add white Gaussian noise to a signal.% [Y, NOISE] = NOISEGEN(X,SNR) adds white Gaussian NOISE to X. The SNR is in dB.NOISE=randn(size(X));NOISE=NOISE-mean(NOISE);signal_power = 1/length(X)*sum(X.*X);noise_variance = signal_power / ( 10^(SNR/10) );NOISE=sqrt(noise_variance)/std(NOISE)*NOISE;Y=X+NOISE;其中X是纯信号,SNR是要求的信噪比,Y是带噪信号,NOISE是叠加在信号上的噪声。 2,把指定的噪声叠加到信号上去 有标准噪声库NOISEX-92,其中带有白噪声、办公室噪声、工厂噪声、汽车噪声、坦克噪声等等,在信号处理中往往需要把库中的噪声叠加到信号中去,而噪声的采样频率与纯信号的采样频率往往不一致,需要采样频率的校准。
function [Y,NOISE] = add_noisem(X,filepath_name,SNR,fs)% add_noisem add determinated noise to a signal.% X is signal, and its sample frequency is fs;% filepath_name is NOISE's path and name, and the SNR is signal to noise ratio in dB.[wavin,fs1,nbits]=wavread(filepath_name);if fs1~=fswavin1=resample(wavin,fs,fs1);endnx=size(X,1);NOISE=wavin1(1:nx);NOISE=NOISE-mean(NOISE);signal_power = 1/nx*sum(X.*X);noise_variance = signal_power / ( 10^(SNR/10) );NOISE=sqrt(noise_variance)/std(NOISE)*NOISE;Y=X+NOISE;其中X是纯信号,filepath_name是指定噪声文件(.wav)的路径和文件名,SNR是要求的信噪比,fs是信号X的采样频率,Y是带噪信号,NOISE是叠加在信号上的噪声。 3,检验带噪信号的信噪比 信噪比的定义为 信号能量 (纯信号)^2 SNR=-----------------=-------------------------- 噪声能量 (带噪信号-纯信号)^2
function snr=SNR_singlech(I,In)% 计算信噪比函数% I :\original signal% In :noisy signal(ie. original signal + noise signal)snr=0;Ps=sum(sum((I-mean(mean(I))).^2));%signal powerPn=sum(sum((I-In).^2)); %noise powersnr=10*log10(Ps/Pn);其中I是纯信号,In是带噪信号,snr是信噪比 以下给出调用上函数的例子可作参考: 例一
clear all; clc; close all;[filename,pathname]=uigetfile('*.wav','请选择语音文件:');[X,fs]=wavread([pathname filename]);[Y,NOISE] = noisegen(X,10);subplot 311; plot(X);subplot 312; plot(NOISE);subplot 313; plot(Y);mn=mean(NOISE)snr=SNR_singlech(X,Y)例二
clear all; clc; close all;[filename,pathname]=uigetfile('*.wav','请选择语音文件:');[filename1,pathname1]=uigetfile('*.wav','请选择噪声文件:');filepath_name=[pathname1 filename1];[X,fs]=wavread([pathname filename]);[Y,NOISE] = add_noisem(X,filepath_name,10,fs);subplot 311; plot(X);subplot 312; plot(NOISE);subplot 313; plot(Y);mn=mean(NOISE)snr=SNR_singlech(X,Y)参考: [1] .