2 Tones

			
				% demonstrate 2-tone signal
				clc;
				clear;

				N = 1024;
				Fs = 1.024e6;

				t = f_timevec(Fs, N);

				% tone 1
				M1 = 117;
				Fi1 = f_Fi(Fs, N, M1);
				fprintf('Fi1 = %e\n', Fi1);
				x1 = sin(2*pi*Fi1*t);

				subplot(3,1,1);
				plot(t, x1);
				title('signal');
				xlabel('time');
				ylabel('amplitude');

				[f1, x1f] = f_fft(N, Fs, x1);
				x1f = mag2db(x1f);
				x1f = x1f - max(x1f);

				% tone 2
				M2 = 81;
				Fi2 = f_Fi(Fs, N, M2);
				fprintf('Fi2 = %e\n', Fi2);
				x2 = sin(2*pi*Fi2*t);

				[f2, x2f] = f_fft(N, Fs, x2);
				x2f = mag2db(x2f);
				x2f = x2f - max(x2f);

				% add 2nd tone
				y = x1 + x2;

				[f, yf] = f_fft(N, Fs, y);

				yf = mag2db(yf);
				yf = yf - max(yf);

				subplot(3,1,2);
				plot(t, y);
				title('signal');
				xlabel('time');
				ylabel('amplitude');

				subplot(3,1,3);
				stairs(f, yf);
				title('fft');
				xlabel('freq');
				ylabel('dB');
			
		
			
				function [Fi] = f_Fi(Fs, N, M)

				% calculate Fi frequency of interest from sampling parameters

				% usage 4 = f_Fi(1.024e3, 1024, 4)

				%    N = number of samples
				%    Fs = sampling rate
				%    M = number of cycles/bin

					Fi = Fs/N * M;

				end
			
		
			
				function [f, xf ] = f_fft(N, Fs, x)

				% single-sided fft
				% usage f_fft(1024, 1.024e3, 4)

				%    N = number of samples
				%    Fs = sampling rate
				%    M = number of cycles/bin
	
					f = Fs/2 * linspace(0, 1, N/2+1); % frequency vector
	
					xf = fft(x,N)/N;
					xf = 2 * abs (xf(1: N/2 +1)); % single sided magnitude

				end
			
		
			
				Fi1 = 1.170000e+05
				Fi2 = 8.100000e+04
			
		
© 2018 Copyright. For individual use only. No liability is accepted for any consequences of using information on Testips.com.