dazemood
dazemood

Reputation: 33

How to create a sinusoidal pattern in Matlab?

Following is my code to generate multiple images of sinusoidal fringe patterns with various shifts.

f1 = 125; f2 = 100; N = 3;
x = linspace(0,pi*480, 640);
y = linspace(0,pi*640, 480);
[X0, Y0] = meshgrid(x,y);
Z1 = 0; Z2 = 2*pi*1/3; Z3 = 2*pi*2/3;
Teq = roundn((f1*f2)/abs(f1-f2), 2); p = 240;
im1 = 127 + 127*cos(2*f1*X0*pi + Z1);
im2 = 127 + 127*cos(2*f1*X0*pi + Z2);
im3 = 127 + 127*cos(2*f1*X0*pi + Z3);
im4 = 127 + 127*cos(2*f2*X0*pi + Z1);
im5 = 127 + 127*cos(2*f2*X0*pi + Z2);
im6 = 127 + 127*cos(2*f2*X0*pi + Z3);

In this case, if I use the above frequencies f1 and f2, I get the following

Sine wave of two frequencies 100 and 125

But if i use a frequencies f1 = 110 and f2 = 140, then I get the follwing sine wave which really looks weird.

Sine wave with f = 110, 140

Why is the period of second picture seems really weird?

The sine fringes are as shown in the following picture. The sine waves shown are taken at pixel y = 240

Sine wave

Upvotes: 1

Views: 1235

Answers (1)

MichaelTr7
MichaelTr7

Reputation: 4757

Setting Number of Sample Points to Fit Sinusoidal Cycles

Aliasing Preface:

Aliasing Example

Aliasing can occur when the sampling frequency used is not high enough to effectively sample the signal with high enough fidelity. An example of this can be sampling a signal that has a frequency of 100Hz. If this 100Hz signal is sampled at a rate of 100Hz you'll effectively see a straight line since only the peaks will be captured/sampled. Simply, the higher the sampling frequency the less ambiguous the sampled signal is. Using the Nyquist criterion can be a good base-point for determining the required frequency as described in the other comment above. You can use the below playground script to experiment.

Playground Script:

%Plot 1%
Sampling_Frequency = 100; %Sampling frequency%
Sampling_Period = 1/Sampling_Frequency;

Start_Time = 0;
End_Time = 5;
t = (Start_Time: Sampling_Period: End_Time);

f = 2; %Frequency of sinusoid%
y = sin(2*pi*f*t);
subplot(2,1,1); plot(t,y,'-o');
title("Sinusoid Sampled at: " + num2str(Sampling_Frequency));
xlabel("t"); ylabel("Amplitude");
axis([Start_Time End_Time -1.2 1.2]);

%Plot 2%
Sampling_Frequency = 9; %Sampling frequency%
Sampling_Period = 1/Sampling_Frequency;

Start_Time = 0;
End_Time = 5;
t = (Start_Time: Sampling_Period: End_Time);


f = 2; %Frequency of sinusoid%
y = sin(2*pi*f*t);
subplot(2,1,2); plot(t,y,'-o');
title("Sinusoid Sampled at: " + num2str(Sampling_Frequency));
xlabel("t"); ylabel("Amplitude");
axis([Start_Time End_Time -1.2 1.2]);

Probably a good idea to use a multiple of pi*480 in this case so that each cycle of the sinusoid (2π) has an equal number of sample points that nicely fit without aliasing. It is also a good idea to plot against another independent variable such as time, t so that you are not effectively plotting against the sample index/number. I wasn't totally sure what was the context of using meshgrid(). If you still need the replications using the repmat() function on im1,im2,im3,im4,im5 and im6 can allow you to create the additional rows you created transitively by using meshgrid().

Frequencies, f1 = 100 and f2 = 140: Sinusoidal Plot

In this case 5000*pi*480 samples were used.

f1 = 110; 
f2 = 140;

Number_Of_Samples = 5000*pi*480;
x = linspace(0,pi*480, Number_Of_Samples);

Z1 = 0;
Z2 = 2*pi*1/3;
Z3 = 2*pi*2/3;

im1 = 127 + 127*cos(2*pi*f1*x + Z1);
im2 = 127 + 127*cos(2*pi*f1*x + Z2);
im3 = 127 + 127*cos(2*pi*f1*x + Z3);
im4 = 127 + 127*cos(2*pi*f2*x + Z1);
im5 = 127 + 127*cos(2*pi*f2*x + Z2);
im6 = 127 + 127*cos(2*pi*f2*x + Z3);

clf;
plot(im1);   
hold on
plot(im2);   
plot(im3);   
plot(im4);   
plot(im5);   
plot(im6);   
xlim([0 1000]);

Plotting with Respect to Another Variable:

Plotting with respect to another vector in this case time, t. Plotting with respect to another variable/vector lifts the constraints of plotting with respect to the sample index. If you take more samples it will not change the horizontal range since its range is determined by the vector, t. In this resolution of 480px by 640px only so many cycles of a sinusoid fit before losing significant fidelity, especially for high-frequency sinusoids. Using the imresize() function helps to resize/decimate the image into the 480px by 640px dimensions.

Sinusoidal Fringe Image

%******************************************************%
%PARAMETERS THAT CAN BE CHANGED%
%******************************************************%
f = 10; %Frequency of sinusoid%
Sampling_Frequency = 1000; %Sampling frequency%
Start_Time = 0;
End_Time = 5;
%******************************************************%

Sampling_Period = 1/Sampling_Frequency;
t = (Start_Time: Sampling_Period: End_Time);
y = sin(2*pi*f*t);
subplot(2,1,1); plot(t,y,'-o');
title("Sinusoid Sampled at: " + num2str(Sampling_Frequency) + "Hz");
xlabel("t"); ylabel("Amplitude");
axis([Start_Time End_Time -1.2 1.2]);

Image_Height = 480;
im1 = repmat(y,Image_Height,1);
im1 = imresize(im1, [480 640]);
subplot(2,1,2); imshow(im1);
title("Resized Image to be 480px by 640px");
xlabel("Width: 640px"); ylabel("Height: 480px");

Final Script: Using the Number of Cycles to Simulate Sinusoids Frequency

This script simply uses the Number_Of_Cycles and transitively End_Time to adjust how much of the sinusoid is plotted for and then uses imresize() to get the 480px by 640px image dimensions. The more of the sinusoid that is plotted the higher the frequency appears in the final image. This way you sample a low-frequency sinusoid with high fidelity and let imresize() decimate the sinusoid to the appropriate size and by plotting more or less of the sinusoid you can get more or less cycles within the image.

Sinusoidal Fringe Pseudo Varying Frequencies

Number_Of_Cycles = 50;
f = 1; 
Sampling_Frequency = 1000;
Start_Time = 0;
End_Time = Number_Of_Cycles*1/f;
Sampling_Period = 1/Sampling_Frequency;
t = (Start_Time: Sampling_Period: End_Time);
y = cos(2*pi*f*t);
Image_Height = 480;
im1 = repmat(y,Image_Height,1);
im1 = imresize(im1, [480 640]);
subplot(2,3,1); imshow(im1);
title(num2str(Number_Of_Cycles) + " Cycles in Image");


%Main parameter to adjust%
Number_Of_Cycles = 100;
End_Time = Number_Of_Cycles*1/f;
Sampling_Period = 1/Sampling_Frequency;
t = (Start_Time: Sampling_Period: End_Time);
y = cos(2*pi*f*t);
im2 = repmat(y,Image_Height,1);
im2 = imresize(im2, [480 640]);
subplot(2,3,2); imshow(im2);
title(num2str(Number_Of_Cycles) + " Cycles in Image");


%Main parameter to adjust%
Number_Of_Cycles = 5;
End_Time = Number_Of_Cycles*1/f;
t = (Start_Time: Sampling_Period: End_Time);
y = cos(2*pi*f*t);
im3 = repmat(y,Image_Height,1);
im3 = imresize(im3, [480 640]);
subplot(2,3,3); imshow(im3);
title(num2str(Number_Of_Cycles) + " Cycles in Image");


%Main parameter to adjust%
Number_Of_Cycles = 10;
End_Time = Number_Of_Cycles*1/f;
t = (Start_Time: Sampling_Period: End_Time);
y = cos(2*pi*f*t);
im4 = repmat(y,Image_Height,1);
im4 = imresize(im4, [480 640]);
subplot(2,3,4); imshow(im4);
title(num2str(Number_Of_Cycles) + " Cycles in Image");


%Main parameter to adjust%
Number_Of_Cycles = 20;
End_Time = Number_Of_Cycles*1/f;
t = (Start_Time: Sampling_Period: End_Time);
y = cos(2*pi*f*t);
im5 = repmat(y,Image_Height,1);
im5 = imresize(im5, [480 640]);
subplot(2,3,5); imshow(im5);
title(num2str(Number_Of_Cycles) + " Cycles in Image");


%Main parameter to adjust%
Number_Of_Cycles = 80;
End_Time = Number_Of_Cycles*1/f;
t = (Start_Time: Sampling_Period: End_Time);
y = cos(2*pi*f*t);
im6 = repmat(y,Image_Height,1);
im6 = imresize(im6, [480 640]);
subplot(2,3,6); imshow(im6);
title(num2str(Number_Of_Cycles) + " Cycles in Image");

Ran using MATLAB R2019b

Upvotes: 2

Related Questions