Matlab信号上叠加噪声和信噪比的计算
开源中国 Team 团队协作平台正式开放 —— http://team.oschina.net
|
1
2
3
4
5
6
7
8
9
|
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; |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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; |
|
1
2
3
4
5
6
7
8
|
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); |
|
1
2
3
4
5
6
7
8
9
|
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) |
|
1
2
3
4
5
6
7
8
9
10
11
|
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) |
原文:http://www.cnblogs.com/daleloogn/p/4168096.html