0001 y=dataSwitching; 0002 Fs = 400; % Sampling frequency 0003 T = 1/Fs; % Sample time 0004 L = length(y); % Length of signal 0005 t = (0:L-1)*T; % Time vector 0006 0007 % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid 0008 %x = 0.7*sin(2*pi*50*t) .* sin(2*pi*12000*t)+0.7*sin(2*pi*50*t); 0009 %y = x + 2*randn(size(t)); % Sinusoids plus noise 0010 %plot(Fs*t(1:50),y(1:50)) 0011 %title('Signal Corrupted with Zero-Mean Random Noise') 0012 %xlabel('time (milliseconds)') 0013 0014 0015 NFFT = 2^nextpow2(L); % Next power of 2 from length of y 0016 Y = fft(y,NFFT)/L; 0017 f = Fs/2*linspace(0,1,NFFT/2+1); 0018 0019 % Plot single-sided amplitude spectrum. 0020 loglog(f,2*abs(Y(1:NFFT/2+1))) 0021 title('Single-Sided Amplitude Spectrum of y(t)') 0022 xlabel('Frequency (Hz)') 0023 ylabel('|Y(f)|') 0024 hold 0025 0026 y=dataNoSwitching; 0027 Fs = 400; % Sampling frequency 0028 T = 1/Fs; % Sample time 0029 L = length(y); % Length of signal 0030 t = (0:L-1)*T; % Time vector 0031 0032 % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid 0033 %x = 0.7*sin(2*pi*50*t) .* sin(2*pi*12000*t)+0.7*sin(2*pi*50*t); 0034 %y = x + 2*randn(size(t)); % Sinusoids plus noise 0035 %plot(Fs*t(1:50),y(1:50)) 0036 %title('Signal Corrupted with Zero-Mean Random Noise') 0037 %xlabel('time (milliseconds)') 0038 0039 0040 NFFT = 2^nextpow2(L); % Next power of 2 from length of y 0041 Y = fft(y,NFFT)/L; 0042 f = Fs/2*linspace(0,1,NFFT/2+1); 0043 0044 % Plot single-sided amplitude spectrum. 0045 loglog(f,2*abs(Y(1:NFFT/2+1)),'r') 0046 %title('Single-Sided Amplitude Spectrum of y(t)') 0047 %xlabel('Frequency (Hz)') 0048 %ylabel('|Y(f)|') 0049 0050 0051 0052