FFT in matlab

			
				clc; clear;

				fs = 1.024e6; % sampling rate
				N = 1024; % no. of samples
				M = 33; % no. of cycles/bins

				fi = fs/N*M; % assume coherency, set a signal freq. of interest
				fprintf('fi = %d\n', fi);

				t = (0:N-1)/fs; % time vector
				x=sin(2*pi*fi*t); % signal

				subplot(3,1,1);
				plot(t,x); % time domain plot
				xlabel('time');
				ylabel('amplitude');
				title('signal');
				ylim([-1 1]);

				xn = awgn(x,15); % add noise with snr = 15dB

				subplot(3,1,2);
				plot(t,xn); % time domain plot with noise
				xlabel('time');
				ylabel('amplitude');
				title('signal with noise');
				ylim([-1 1]);

				y = fft(xn,N); % N-point FFT
				Y = abs(y(1:N/2+1)); % single-sided
				YdB = mag2db(Y); % convert to dB
				YdB = YdB - max(YdB); % scale to 0dB
				f = fs/2 * linspace(0,1,N/2+1); % freq vector

				subplot(3,1,3);
				stairs(f,YdB, 'b'); % FFT plot
				xlabel('freq');
				ylabel('magnitude');
				title('FFT');
				grid on;
			
		
© 2018 Copyright. For individual use only. No liability is accepted for any consequences of using information on Testips.com.