clf;
%fm=100;f0=2.4e10;B=2.4e8;FR=320R;FD=160v;
Fs = 130000; % Sampling frequency
Ts = 1/Fs; % Sampling period
N = 512; % Length of signal
n=0:N-1;
%t1 = (0:1:N-1)*Ts; % Time vector
t1=n/Fs;
%y = wgn(m, n, p);
% Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz sinusoid of amplitude 1.
S = sqrt(2)*sqrt(15)*sin(2*pi*50000*t1) + sqrt(2)*sqrt(13)*sin(2*pi*14400*t1);
% Corrupt the signal with zero-mean white noise with a variance of 4.
NOISE=wgn(1, 512, 7);
X = S+NOISE;
sigPower = sum(abs(S).^2)/length(S); %求出信号功率
noisePower=sum(abs(X-S).^2)/length(X-S); %求出噪声功率
SNR=10*log10(sigPower/noisePower) ;
% Plot the noisy signal in the time domain. It is difficult to identify the frequency components by looking at the signal X(t).
%对信号进行快速Fourier变换
Y = fft(X); %求得Fourier变换后的振幅
mag1=abs(2*Y/N);
P1 = mag1(1:N/2);
%P1=20*log10(abs(P1));
%P1(2:end-1) = 2*P1(2:end-1);
f1=(1:N/2)*Fs/N; %频率序列
figure(1);
plot(1000*t1,X);title('Signal Corrupted with Zero-Mean Random Noise');xlabel('t (milliseconds)');ylabel('X(t)');
figure(2);
plot(f1,P1,'r'); xlabel('频率/Hz');ylabel('dbv');title(': FFT','color','r');