% alien_voices_4.m
% by Richard Sikora 23 August 2003.

% To show the effect of ring modulation, that is to multiply a 
% sinusoidal input signal by a sinusoidal modulation frequency to
% provide sum and difference frequencies.

% To experiment with ring modulation, change the two frequencies below
% The two frequencies when added together should not exceed 5000.

input_frequency = 500;
modulation_frequency = 4000;

t = 0: 0.0001: 0.1;  % Time range. Step size affects frequency range

N = (0.1/0.0001)+1; % Calculate size of time array

% Multiply each value in input_frequency array by modulation_frequency 
% For better resolution on the display, the each frequency has been divided by 10
for i = 1 : N
  input(i) = sin( 2 * pi * input_frequency/10 * t(i) ) ;
  % To add a second harmonic to the input, uncomment the following line
  %input(i) = input(i) + 0.7 * sin( 2 * pi * 2 * input_frequency/10 * t(i) ) ;
  modulation(i) = sin ( 2 * pi * modulation_frequency/10 * t(i));
  y(i) = input(i) * modulation(i);
end    

% Plot modulated frequency on top pane

subplot(2,1,1);
plot ( t, y ), grid on
xlabel ('Time in ms');
ylabel ('Magnitude');
title ('500 Hz Input Signal Modulated by 4000 Hz');
set (gca, 'xlim', [0, 0.02])
set (gca, 'XTickLabel', [0 0.5 1.0 1.5 2.0])
set (gca, 'ylim', [-1.5, 1.5])

% Multiply each value in input_frequency array by modulation_frequency 
% This time the input frequency is not divided by 10.
for i = 1 : N
  input(i) = sin( 2 * pi * input_frequency * t(i) ) ;
  % To add a second harmonic to the input, uncomment the following line
  %input(i) = input(i) + 0.7 * sin( 2 * pi * 2 * input_frequency * t(i) ) ;
  modulation(i) = sin ( 2 * pi * modulation_frequency * t(i));
  y(i) = input(i) * modulation(i);
end   

% Perform 1024 Point FFT on modulated signal y
Y = fft( y,1024);

% Calculate power in signal
Pyy = Y.* conj(Y)/1024;

% Scale frequency range for frequency f
f = 10000 * (0:512)/1024;

% Plot output of FFT on second pane
subplot(2,1,2);
plot ( f, Pyy(1:513));
xlabel ('Frequency');
ylabel ('Power');
title ('Frequency Spectrum of Modulated Signal using FFT');
set (gca, 'xlim', [0, 5000])
set (gca, 'XTick', [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000])
set (gca, 'ylim', [0, 100])
