简体   繁体   English

如何在 Matlab 中创建正弦模式?

[英]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在这种情况下,如果我使用上述频率 f1 和 f2,我会得到以下结果

两个频率 100 和 125 的正弦波

But if i use a frequencies f1 = 110 and f2 = 140, then I get the follwing sine wave which really looks weird.但是如果我使用频率 f1 = 110 和 f2 = 140,那么我会得到看起来很奇怪的后续正弦波。

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显示的正弦波取自像素 y = 240

正弦波

Setting Number of Sample Points to Fit Sinusoidal Cycles设置采样点数以拟合正弦循环

Aliasing Preface:别名序言:

别名示例

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.一个例子是对频率为 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.如果此 100Hz 信号以 100Hz 的速率采样,您将有效地看到一条直线,因为只会捕获/采样峰值。 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.在这种情况下使用pi*480的倍数可能是个好主意,这样正弦曲线 (2π) 的每个周期都具有相等数量的采样点,可以很好地拟合而不会出现混叠。 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.对另一个自变量(例如时间、 t进行绘图也是一个好主意,这样您就不能有效地针对样本索引/数字进行绘图。 I wasn't totally sure what was the context of using meshgrid() .我不完全确定使用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() .如果您仍然需要在im1im2im3im4im5im6上使用repmat()函数进行im1im6可以允许您通过使用meshgrid()传递创建的其他行。

Frequencies, f1 = 100 and f2 = 140:频率,f1 = 100 和 f2 = 140: 正弦图

In this case 5000*pi*480 samples were used.在这种情况下,使用了5000*pi*480样本。

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 .在这种情况下绘制另一个向量时间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 .如果您采集更多样本,它不会改变水平范围,因为其范围由向量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.在这种480px by 640px分辨率下480px by 640px在失去显着保真度之前,只有如此多的正弦拟合周期,特别是对于高频正弦曲线。 Using the imresize() function helps to resize/decimate the image into the 480px by 640px dimensions.使用imresize()函数有助于将图像调整/ 480px by 640px480px by 640px尺寸。

正弦边缘图像

%******************************************************%
%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.此脚本仅使用Number_Of_Cycles和传递性的End_Time来调整绘制的正弦曲线的数量,然后使用imresize()获得480px by 640px图像尺寸。 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.通过这种方式,您可以以高保真度对低频正弦曲线进行采样,并让imresize()将正弦曲线抽取到适当的大小,通过绘制或多或少的正弦曲线,您可以在图像中获得更多或更少的周期。

正弦边缘伪变频

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使用 MATLAB R2019b 运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM