使用JSF进行弹簧注射(Spring Injection with JSF)
我在使用JSF项目中的springframework注入带有注释的Beans时遇到了问题。
我想我的确完全基本上我发现到目前为止我发现的每一个教程都说我应该这样做但是当我将我的战争发布到tomcat并尝试使用LoginBean中的loginService时,我得到一个NullPointerException。 有任何想法吗?我的豆(缩写):
package de.homer.server.config.beans; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import de.homer.server.config.user.LoginService; @Component @Scope("session") public class LoginBean implements Serializable { @Autowired LoginService loginService; public final String doLogin() { // try to use loginService here // if I set breakpoint here I can see that loginService is null } }
我的服务(缩短):
package de.homer.server.config.user; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component public class LoginService { }
我的applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="de.homer.server.config.beans" /> <context:component-scan base-package="de.homer.server.config.user" /> </beans>
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>homeR</display-name> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> <!-- Spring context --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- JSF mapping --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map these files with JSF --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app>
我的faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> <faces-config> <application> <variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver> </application> </faces-config>
如果它重要我的pom.xml的摘录
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.0.RELEASE</version> </dependency>
最后但并非最不重要的一部分日志:
DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [/home/stonedsquirrel/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/homeR-server-runtime/WEB-INF/classes/de/homer/server/config/user/LoginService.class] ... DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'loginService' DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'loginService' DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'loginService' to allow for resolving potential circular references DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'loginService'
和堆栈跟踪:
WARNING: #{loginBean.doLogin}: java.lang.NullPointerException javax.faces.FacesException: #{loginBean.doLogin}: java.lang.NullPointerException at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) ... 24 more Caused by: java.lang.NullPointerException at de.homer.server.config.beans.LoginBean.doLogin(LoginBean.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 25 more Jan 16, 2012 9:42:26 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/homer] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException at de.homer.server.config.beans.LoginBean.doLogin(LoginBean.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)
I'got a problem with Injecting Beans with annotations using the springframework in a JSF Project.
I think I did exactly what basically every Tutorial I've found so far says I should do but when I publish my war to tomcat and try to use the loginService from the LoginBean I get a NullPointerException. Any Ideas?my Bean (shortened):
package de.homer.server.config.beans; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import de.homer.server.config.user.LoginService; @Component @Scope("session") public class LoginBean implements Serializable { @Autowired LoginService loginService; public final String doLogin() { // try to use loginService here // if I set breakpoint here I can see that loginService is null } }
my service (shortened):
package de.homer.server.config.user; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component public class LoginService { }
my applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="de.homer.server.config.beans" /> <context:component-scan base-package="de.homer.server.config.user" /> </beans>
my web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>homeR</display-name> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> <!-- Spring context --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- JSF mapping --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map these files with JSF --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app>
my faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> <faces-config> <application> <variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver> </application> </faces-config>
and if it matters an excerpt of my pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.0.RELEASE</version> </dependency>
last but not least a part of the log:
DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [/home/stonedsquirrel/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/homeR-server-runtime/WEB-INF/classes/de/homer/server/config/user/LoginService.class] ... DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'loginService' DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'loginService' DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'loginService' to allow for resolving potential circular references DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'loginService'
and the stack trace:
WARNING: #{loginBean.doLogin}: java.lang.NullPointerException javax.faces.FacesException: #{loginBean.doLogin}: java.lang.NullPointerException at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) ... 24 more Caused by: java.lang.NullPointerException at de.homer.server.config.beans.LoginBean.doLogin(LoginBean.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 25 more Jan 16, 2012 9:42:26 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/homer] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException at de.homer.server.config.beans.LoginBean.doLogin(LoginBean.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)
原文:https://stackoverflow.com/questions/8875015
最满意答案
这些值不是“出路”,它们与零到机器精度一致。 Python通常具有53位的精度:
https://docs.python.org/2/tutorial/floatingpoint.html
这个二进制表示对应于~1e-16,这是您在正弦函数中应该与零对应的值中看到的顺序。
These values are not "way-out-there," they are consistent with zero to machine precision. Python typically has precision to 53 bits:
https://docs.python.org/2/tutorial/floatingpoint.html
The binary representation for this corresponds with ~1e-16, which is the order you are seeing in the values that should correspond with zero in your sine function.
相关问答
更多-
有点不清楚你在问什么...也许你想试试这个: import matplotlib.pyplot as plt import numpy as np Bias=5; x = np.linspace(-20, 20, 1000); y = np.abs(np.sin(x)) y = y + Bias plt.plot(x, y) plt.grid() plt.show() 或这个: import matplotlib.pyplot as plt import numpy as np Bias=5; ...
-
给定单个正弦波值和周期递归创建正弦波(Recursively create a sine wave given a single sine wave value and the period)[2023-11-23]
让我们从一些三角身份开始: sin(x)^2 + cos(x)^2 == 1 sin(x+y) == sin(x)*cos(y) + sin(y)*cos(x) cos(x+y) == cos(x)*cos(y) - sin(x)*sin(y) 给定x点处的正弦和余弦,在预先计算sd = sin(d)和cd = cos(d)后,我们可以准确地计算出尺寸为d的步长后的值: sin(x+d) = sin(x)*cd + cos(x)*sd cos(x+d) = cos(x)*cd - sin(x)*sd 给 ... -
正如评论员@NeilForrester指出的那样, FFT是实现这一目标的方法。 编写自己的高效FFT并不容易,但是如果使用Objective-C , Accelerate框架的vDSP例程提供了一种直接的方法 - 由于使用了UnsafePointer和UnsafeMutablePointer参数,因此在Swift中不那么简单。 这是一个使用FFT的简单Swift示例。 import Foundation import Accelerate public struct GFFT { let siz ...
-
在WPF中绘制正弦波(Draw Sine Wave in WPF)[2024-02-17]
在使用Math.Sin函数计算的点之间绘制线条。 您必须决定每个周期使用多少个点,这是绘图速度和精度之间的折衷。 据推测,您还需要缩放幅度以适应屏幕上的区域,因为Sin函数将返回+1和-1之间的值。 Draw lines between points which you calculate with Math.Sin function. You'll have to decide how many points per cycle to use, a compromise between drawing s ... -
在Tkinter中移动正弦波(Moving a sine wave in Tkinter)[2021-11-07]
我不明白'移动',但最有可能使用matplotlib。 import matplotlib.pyplot as plt fig = plt.figure(frameon=False) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.plot(range(10)) plt.show() I don't understand 'moving' but matplotlib could most likely be used. import matplot ... -
Python中正弦波计算中出现意外/不需要的结果(Unexpected/Unwanted results in sine wave calculation in Python)[2022-11-03]
这些值不是“出路”,它们与零到机器精度一致。 Python通常具有53位的精度: https://docs.python.org/2/tutorial/floatingpoint.html 这个二进制表示对应于~1e-16,这是您在正弦函数中应该与零对应的值中看到的顺序。 These values are not "way-out-there," they are consistent with zero to machine precision. Python typically has precisio ... -
采样频率正弦波(Sampling Frequency sine wave)[2023-11-21]
对于数字FIR滤波器,您应该有一定数量的“抽头”或定义滤波器的滤波器系数。 点击次数取决于以下几点: 1.)信号采样频率 2.)滤波器的频率响应 鉴于您似乎在示例中选择了任意Fs ,我只能假设您没有在fdatool界面上选择Fs 。 您应该确保滤波器的采样频率和您生成的信号的采样频率相同。 For a digital FIR filter you should have a certain number of 'taps', or filter coefficients which define the f ... -
如果需要效率,您可以预加载440hz(44100/440)正弦波形查找表并在其周围循环而无需进一步映射或预加载1hz(44100/44100)正弦波形查找表并循环通过递增相位计数器,跳过样本达到440hz。 使用查找表应该比计算sin()更快。 方法A(使用440hz正弦波形): #define SAMPLES 1024 #define FREQUENCY 440 #define SAMPLING_RATE 44100 #define WAVEFORM_LENGTH (SAMPLING / FREQUENC ...
-
考虑这个问题的最简单方法是变换坐标系,按两点之间的角度旋转,按它们之间的距离缩放并按第一点平移(假设正弦从0,0开始)。 OP已经指定他不只是想绘制曲线(在这种情况下,所有人都需要做的是将变换应用于图形上下文),而是在SpriteKit SKAction.followPath调用中使用曲线,所以变换必须应用于路径中的坐标,而不是上下文。 这是使用CGPath而不是UIBezierPath的解决方案,但它们是等效的,您只需通过let uip = UIBezierPath(cgPath: path)即可获得UI ...
-
绘制正弦波时出错(Error with plotting a sine wave)[2021-02-13]
你正在混淆。 您的采样频率需要远高于您想要绘制的最大频率以获得“平滑”图。 对于5 kHz正弦波,您需要采样频率至少为50 kHz,即使那时也不是很多(每个周期只有10个数据点)。 100 kHz将为每个周期提供20个数据点,这可能看起来很好: You are getting aliasing. Your sampling frequency needs to be much, much higher than that the maximum frequency you want to plot to g ...