MATLAB: I am generating a sine wave with a phase difference. The peak amplitude however seems to scale with the phase difference, which should not be case. Any suggestions to where this code is going wrong

sinewavewave phasedifference

Frequency = 15 ; % in Hertz
SamplingFrequency = 100*Frequency; % in Hertz. Note: Increasing the sampling frequency does not help.
Simulationtimestep = 1/SamplingFrequency; % Simulation time-step
Simulationtime=0:Simulationtimestep:1-Simulationtimestep; % Simulation time
Phasediff = deg2rad(20); % in radians
Amplitude = 2;
Wave1 = Amplitude*sin(2*pi*Frequency*Simulationtime); % Reference Wave
Wave2 = Amplitude*sin(2*pi*Frequency*Simulationtime+Phasediff ); % Wave with phase difference
disp(max(Wave1));%Should be equal to 2
Wave1Peaks = numel(find(Wave1==Amplitude)); % Should be equal to the frequency (15)
disp(max(Wave2)); %Should be equal to 2
Wave2Peaks = numel(find(Wave2==Amplitude)); % Should be equal to the frequency (15)

Best Answer

hi
problem solved - see below
I also increased your sampling frequency (360 x 15 Hz) so that you get the best results accuracy for 1° changes in your phase diff value.
clc
Frequency = 15 ; % in Hertz
SamplingFrequency = 360*Frequency; % in Hertz. Note: Increasing the sampling frequency does not help.
Simulationtimestep = 1/SamplingFrequency; % Simulation time-step
Simulationtime=0:Simulationtimestep:1-Simulationtimestep; % Simulation time
Phasediff = pi/180*(22); % in radians
Amplitude = 2;
Wave1 = Amplitude*sin(2*pi*Frequency*Simulationtime); % Reference Wave
Wave2 = Amplitude*sin(2*pi*Frequency*Simulationtime+Phasediff ); % Wave with phase difference
disp(max(Wave1));%Should be equal to 2

Wave1Peaks = numel(find(Wave1==Amplitude)); % Should be equal to the frequency (15)

disp(max(Wave2)); %Should be equal to 2
Wave2Peaks = numel(find(Wave2==Amplitude)); % Should be equal to the frequency (15)
% example of findpeaks usage for Wave1
Wave1Peaks_ind = findpeaks(Wave1); % indexes of peak values

Wave1Peaks_Time = Simulationtime(Wave1Peaks_ind)
Wave1Peaks_Amplitude = Wave1(Wave1Peaks_ind)
% example of findpeaks usage for Wave2
Wave2Peaks_ind = findpeaks(Wave2); % indexes of peak values
Wave2Peaks_Time = Simulationtime(Wave2Peaks_ind)
Wave2Peaks_Amplitude = Wave2(Wave2Peaks_ind)
function n = findpeaks(x)
% Find peaks.
% n = findpeaks(x)
n = find(diff(diff(x) > 0) < 0);
u = find(x(n+1) > x(n));
n(u) = n(u)+1;