Active anemosensing hypothesis: how flying insects could estimate ambient wind direction through sensory integration and active movement

Estimating the direction of ambient fluid flow is a crucial step during chemical plume tracking for flying and swimming animals. How animals accomplish this remains an open area of investigation. Recent calcium imaging with tethered flying Drosophila has shown that flies encode the angular direction of multiple sensory modalities in their central complex: orientation, apparent wind (or airspeed) direction and direction of motion. Here, we describe a general framework for how these three sensory modalities can be integrated over time to provide a continuous estimate of ambient wind direction. After validating our framework using a flying drone, we use simulations to show that ambient wind direction can be most accurately estimated with trajectories characterized by frequent, large magnitude turns. Furthermore, sensory measurements and estimates of their derivatives must be integrated over a period of time that incorporates at least one of these turns. Finally, we discuss approaches that insects might use to simplify the required computations, and present a list of testable predictions. Together, our results suggest that ambient flow estimation may be an important driver underlying the zigzagging manoeuvres characteristic of plume tracking animals’ trajectories.


Introduction
Estimating the direction of ambient flow is a critical step for both swimming and flying animals during chemical plume tracking behaviours [1]. Without access to stationary flow sensors, however, the ambient direction of fluid flow cannot be directly measured [2]. How animals resolve this challenge remains an open area of investigation. Recent experimental work with fish has shown that they use temporal changes of flow from mechanosensory measurements to deduce flow direction during rheotaxis [3]. Comparatively less is known about how insects resolve this challenge while flying in windy environments.
Flying insects can measure apparent wind, corresponding to the vector sum of the ambient wind and motion-induced wind. Decoupling apparent wind into its component parts is a straightforward vector subtraction if given absolute trajectory information in the same units as apparent wind. Insects, however, do not have direct access to such information. It is conceivable for an insect to estimate absolute ground speed using optic flow and inertial measurements (e.g. [4]), or to estimate ambient wind direction using a combination of optic flow and apparent wind [5]. However, both such approaches rely on calibrated measurements of the magnitude of optic flow and other modalities. Optic flow responses of insects, however, have depth- [6], texture- [7], temperature- [8] and state- [9] dependent response properties, resulting in constantly varying and unknown sensor gains. The magnitude of other sensor modalities will have similar time-dependent properties.
Through calcium imaging experiments, it has been found that a central part of insects' brains, the central complex, encodes two-dimensional angular compass information such as head orientation from visual landmarks [10] or polarization [11], apparent wind angle from their antennae [12,13], and angular direction of motion from vision [14][15][16]. Neuroanatomical studies suggest that similar sensory encodings exist in many invertebrates [17]. These two-dimensional angular encodings do not suffer from the same calibration challenges as magnitude measurements would, since the angular directions are equivalent to calculating the ratio of two perpendicular magnitude measurements, eliminating the unknown sensor gain. This feature and the physical overlap in the brain of the aforementioned sensory modalities presents an attractive hypothesis that perhaps they play a central role in insects' ability to estimate ambient wind direction.
One strategy that relies solely on such angular measurements is called visual anemotaxis [18]. This algorithm would allow insects to orient upwind by turning until the angle of perceived apparent wind is aligned with the angle of motion. Behavioural experiments with free and tethered flies, however, suggest that their turns are often open-loop and ballistic in nature, and not actively controlled throughout the turn itself [19][20][21]. Furthermore, their free flight plume tracking behaviour indicates that they are capable of moving crosswind at arbitrary body orientations with respect to the wind direction [22]. Together, these behavioural experiments suggest that insects may be capable of estimating the direction of ambient wind throughout their flight trajectories.
A recent nonlinear observability analysis of the three key sensor modalities encoded in the central brain of flies-orientation, apparent wind angle and direction of motion-found that this sensor suite is mathematically sufficient for estimating the direction of ambient wind without explicitly requiring upwind orientation [23]. Estimating ambient wind direction in this manner, however, is only feasible for certain trajectories, such as those where the insect changes course direction.
This manuscript provides a detailed mathematical and simulation analysis to determine which trajectories offer the most accurate ambient wind direction estimates, as well as which computations are actually required to make these estimates. First, we establish the mathematical relationships required to estimate ambient wind direction that are applicable to any estimator using the three measurements we consider. Second, we describe an optimal framework for estimating the ambient wind direction and validate the robustness of our approach using data from a flying drone. Third, we use our optimal framework to determine which types of trajectories are capable of providing the most accurate estimates under a variety of conditions. By using an optimal framework to answer this question, we ensure that our results are equally applicable to other computationally simpler estimation algorithms designed for the same task. Finally, we discuss how insects might simplify the computations required to estimate ambient wind direction.

Mathematics of estimating ambient wind direction
Inspired by the two-dimensional angular encodings found in Drosophila, our analysis will focus on the plane parallel to the ground. Since this is also the plane where the largest changes in wind direction are usually observed [24], it also represents the most important angle that insects might need to estimate. Additional considerations for extensions to three dimensions are given in [23].
In this section, we first review visual anemotaxis, and then develop a general framework for understanding how ambient wind direction ζ(t) could be estimated. In both cases, we restrict our analysis to only consider the kinematic relationships and time history of three angular measurements (figure 1), hðtÞ ¼ ½fðtÞ, gðtÞ, cðtÞ T , ð2:1Þ where ϕ(t) is the head orientation with respect to a global coordinate frame, γ(t) is the angular direction of airspeed relative to the head coordinate frame (equivalent to apparent wind + 180°), and ψ(t) is the angular direction of motion relative to the head coordinate frame. For notational simplicity, we drop the (t) going forward. We begin by defining the relationship between the angular measurements h, the groundspeed (g) and wind speed (w) provided by the Law of sines (figure 1) This relationship highlights the challenge of using the airspeed direction γ to estimate the wind direction, as any changes in the value of γ could be the result of changes in ζ, g, or w, none of which are available to the insect.
Since we are only concerned with estimating ζ, we simplify the relationship by defining v = g/w, a nondimensional velocity describing the ratio of ground speed and wind speed, yielding v sinðg À cÞ ¼ sinðg þ f À zÞ: ð2:3Þ

Visual anemotaxis (a review)
Before defining our framework for active anemosensing, we review the mathematics of the visual anemotaxis hypothesis. Consider what would happen if a fly were to actively control its orientation (ϕ) and direction of motion (ψ) based on sensory feedback and where k ϕ and k ψ are both constant gains that determine the rate of change in ϕ and ψ. Together, these control laws will ensure royalsocietypublishing.org/journal/rsif J. R. Soc. Interface 19: 20220258 that the fly is oriented in the direction of its airspeed (γ = 0) and moving in the direction it is oriented (ψ = 0). For simplicity, the following analysis uses k ϕ = k ψ = 1; the same qualitative results hold for different choices of k ϕ,ψ (see electronic supplementary material, figure S1). To understand how these control laws would affect the fly's movement relative to the ambient wind direction, we introduce a new variable for the course direction relative to the ambient wind direction, Next, we calculate the derivative of c z , under the assumption that the fly changes its course direction much faster than any changes in the ambient wind direction (i.e. _ z % 0), We now rearrange equation (2.5) as f À z ¼ c z À c and substitute it into equation (2.3), followed by a substitution of _ c z for the two instances of γ − ψ yielding the following, We now solve equation (2.7) for _ c z as follows, This expression allows us to analyse the stable and unstable equilibria for global course direction in relation to the ambient wind direction for different values of v using a onedimensional phase portrait (figure 2). This analysis shows that although moving downwind and upwind are both equilibria, upwind movement (c z ¼ +p) is the only stable outcome. During behaviours such as migration, insects may choose to fly at an angle relative to the wind [25], rather than orient upwind. Such behaviour could be accomplished using visual anemotaxis, but the insect would not have access to what this angle is by using visual anemotaxis alone (see electronic supplementary material, figure S2).

Active anemosensing
In contrast to visual anemotaxis, we now develop a framework for estimating the ambient wind direction that does not first require finding upwind. To begin, we simplify the measurement space by introducing two new variables (α: direction of air velocity relative to the global frame, and δ: 'slip' angle between the air velocity and ground velocity vectors), and rewrite equation (2.3), Equation (2.9) has two unknowns v and ζ, resulting in an under-determined system given the measurements α and δ. To uniquely determine ζ, we add the following constraint from the time-derivative of equation (2.9), þ _ a sin a sin z À _ z cos a cos z, ð2:10Þ Together, equations (2.9c)-2.11 fully constrain ζ provided that derivatives of the measurements can be calculated or measured. Thus, in principle a variety of optimization methods could be used to estimate ζ using these relationships as constraints. To understand which trajectories lead to accurate estimates under varying levels of sensory noise and for different natural wind dynamics, we develop an optimal framework for estimating ζ using convex methods in the following section.

Active anemosensing using convex optimization
In this section, we construct an objective function of the form ℓ = | |L| | + | |dL| | that is linear with respect to the unknowns such that it can be efficiently solved using traditional convex optimization tools.
The terms in equation (2.10) containing _ z include the product of two unknowns, resulting in a non-convex formulation. Thus, we introduce the assumption that the dynamics of the trajectory are faster than the dynamics of the wind. That is, for any given time window of length τ, we assume that _ z % 0, but the other derivative terms may all be non-zero and time-varying.
We can now build a convex estimator that incorporates measurements from a given window of time [k − τ, k] to estimate the average wind direction for that window (z k ). To build our estimator, we define the following two loss functions derived from equations (2.9) and (2.10): L ¼ v sin d À sin a cos z þ cos a sin z ð2:12aÞ  royalsocietypublishing.org/journal/rsif J. R. Soc. Interface 19: 20220258 This optimization problem will find the best estimate for a single value ofẑ k , and a time series forv, given sensory measurements from a window in time from k − τ to k. Because the errors in the derivative estimates will almost always be larger than the errors in the undifferentiated values, we apply a 2-norm to dL and a 1-norm to L. Although the optimization problem given in equation (2.13) is convex, it does not follow disciplined convex programming (DCP) rules because of the trigonometric functions. To efficiently solve the problem, we can recast it as two DCP problems. For the first, we divide L, dL by cos z, absorbing this constant term into v, denoted as v c , and then optimizing to find tan z using the following expressions: Extractingẑ c from tanẑ c results in an ambiguity of +np, n [ Z, which we resolve using the following constraint derived from equation (2.9): signðsin dÞ ¼¼ signðsinða À zÞÞ: ð2:15Þ If z happens to be close to p=2 + np, n [ Z, then dividing by cosz can result in large errors. Thus, to improve accuracy, we solve a second optimization problem where we divide by sin z instead of cosz and optimize to find cotẑ using the following modified expressions: and choose the value ofẑ from the choices fẑ c ,ẑ s g that corresponds to the smaller of the two values for ' c min , ' s min . This results in choosing the value ofẑ that is most consistent with the sensor measurements over the given window. Note that because we estimate an average wind direction given measurements from a segment of time τ-long, the estimateẑ k is delayed by τ/2. In our analysis, we automatically adjust for this delay in order to compare the estimate to the true values, however, in a real-time application smaller values of τ should be preferred.

Practical considerations and time constants
To mitigate the effects of noise, we assimilate measurement data across time using three time constants (figure 3a). These time constants are fundamental to our overall active anemosensing hypothesis, and not a specific feature of the convex solver we implement. With respect to our convex solver, the time constants define the window over which the solver is defined to operate. Therefore, the impact of these time constants on the resulting wind direction estimates should generalize beyond our particular convex solver.
(1: ω) We estimate smoothed values for the measurements ( †), and their derivatives (_ †), using a Butterworth filter with a specified cut-off frequency, ω [26] to reduce noise in the estimates. The cut-off frequency effectively describes the maximum frequency of expected changes in the measurement. Since our algorithm assumes that changes in direction of motion occur at a higher rate than changes in wind direction, a good choice is generally an ω = turning frequency. (2: τ) To estimate the ambient wind direction, we assimilate sensory measurements over a window in time of size τ.  Figure 3. (a) Visual representation of the three time constants (ω, τ, T) fundamental to our active anemosensing hypothesis. (b) Outline of our convex ambient wind direction estimation algorithm. Note that the convex optimization steps in the orange box could be replaced with other workflows that are less optimal but more computationally efficient by using simplifications and heuristics, as we discuss in the Discussion.
Figure 3b provides a visual summary of our entire convex estimation framework, taking these three practical considerations and time constants into account.

Methods
This section provides an overview of our implementation of the convex estimation framework described in the prior section, realworld wind measurements, candidate trajectory selections, simulations, and our drone platform for validating the robustness of our estimator framework.

Implementation of estimation framework
To solve the convex optimization problems outlined in figure 3, we used the open-source Python package pynumdiff to estimate Butterworth smoothed derivatives [27], and cvxpy [28] with the proprietary MOSEK solver [29]. For computational efficiency, we estimated wind directions for every 10th time step (6 Hz for our validation experiment and 1 Hz for our simulation experiments).

Real-world wind measurements
We collected wind direction and speed data using two threedimensional ultrasonic anemometers (Trisonica mini, Anemoment, Longmont, CO, USA) operating at 10 Hz positioned orthogonal to one another to ensure accurate readings of both horizontal and vertical wind components. We collected data in an open desert environment (figure 4a). The vertical wind component was minimal, whereas the horizontal wind varied, with dynamic periods characterized by low speeds and frequent changes in direction, as well as consistent periods with higher wind speeds and near-constant direction (figure 4b).
To visualize the likelihood of a shift in wind direction over time, we took the difference between a point in the recorded time series and a point at various time lags between 0.1 and 1000 s. The differences in wind direction at each lag were computed by moving both forward and backward in time, and those values were averaged. By repeating this analysis throughout the time series, we created a frequency distribution. We then used this distribution to generate a heat map, which demonstrates the approximate probability that magnitude that wind direction will change at any given time over a period of 1000 s. In our dynamic wind segment, changes in wind direction became increasingly likely after 10-50 s (figure 4c), whereas for the consistent wind segment large changes in wind direction were rare for the entire time series (figure 4d ).

Candidate trajectories
To design our candidate trajectories, we took inspiration from previously published data on plume tracking fruit flies flying in a laminar flow wind tunnel [22]. In the presence of attractive odours, fruit flies perform upwind and crosswind flight manoeuvres referred to as surges and casts (figure 5a). Although this behaviour has received substantial attention, aspects of their flight dynamics have escaped scrutiny, which we briefly address here by reanalysing these previously published data. To focus our analysis on plume tracking individuals, we selected trajectories with durations greater than 1 s during which an odour (ethanol) was encountered at least once while moving (on average) upwind in 0.4 m s −1 wind, resulting in 700 trajectories. These flies exhibited regular changes in course direction at a rate of approximately 0.5 Hz (figure 5b), corresponding to approximately one left and one right turn every 2 s.
Flies generally accomplish changes in course direction using rapid turns called saccades [19][20][21]30]. To understand the dynamics of the saccades during plume tracking specifically, we isolated individual turns from the 700 trajectories by selecting 1 s long snippets during which the flies exhibited a minimum angular velocity of 230°/s, and no greater than 460°/s prior or after the turn (figure 5c). This threshold choice is consistent with prior saccade analyses of freely flying flies which used a threshold of 300°/s [19]. The amplitude of their turns range from small adjustments to 180°changes (figure 5d ), with an overall average of 110°. As with flies flying in the absence of odour or wind [19], the saccade magnitude is strongly correlated with the peak angular velocity during the turn, suggesting they have an internal model of their new desired heading prior to initiating the turn, an observation that goes against the prediction of the visual anemotaxis hypothesis. In order to make these rapid changes in direction flies first decelerate, and then accelerate as they exit the turn (figure 5e), though there is substantial additional variability in velocity throughout their trajectories.  royalsocietypublishing.org/journal/rsif J. R. Soc. Interface 19: 20220258 Based on the behaviour of plume tracking flies, we primarily consider trajectories stereotyped by regular changes in direction accompanied by synchronized changes in ground speed. We consider three categories of trajectories with small (approx. 20°), medium (approx. 90°) and large (approx. 160°) turn angles. For the small turn trajectories, the magnitude of each turn is selected randomly from a uniform distribution of 16-24°. For medium and large turn trajectories, these uniform distributions were 72-108°and 144-171°, respectively. We applied a Gaussian smoothing kernel to these piecewise constant course directions, resulting in smooth yet still concentrated turns. Although plume tracking insects do exhibit vertical movements, we restricted our candidate trajectories to movements parallel to the ground plane for simplicity. For the orientation of our simulated flies, we consider two cases inspired by the active control required by visual anemotaxis: . Finally, we also consider constant ground speed trajectories.

Simulations
To determine the accuracy of ambient wind estimates for different trajectories in natural wind conditions, we simulated our trajectories in both the 25 min long dynamic-and 12 min long consistent-wind scenarios at a temporal resolution of 0.1 s (the time resolution of our wind data). To focus our analysis on the impact of the trajectory shape, rather than the feedback control necessary to achieve that trajectory, we programmed the trajectories kinematically, thus changes in wind speed and direction did not have any effect on the shape of the trajectories in our simulations. We justify this decision by noting that flies exhibit exceptional visual and mechanosensory feedback mechanisms for gust rejection [31]. We extracted the three sensory modalities under consideration, and applied zero-mean Gaussian noise with standard deviations ranging between 17 and 67 . All simulations were performed with the same initial random seed to ensure a fair comparison across different conditions. Next, we smoothed the sensor measurements and estimated their derivatives using a Butterworth filter and ran the estimates through our optimal estimation framework for different values of τ and T. For each individual wind direction estimate, we calculate the absolute value of the error between the estimate and the true wind direction.

Drone hardware for validation experiments
Platform. To validate our estimation approach, we built a quadrotor drone using a Holybro X500 frame (figure 6a). The drone was controlled by a PixHawk flight controller. The PixHawk was flashed with PX4 software and configured using QGroundControl software. The drone was powered by a 14.8 V 5200 mAh 60C 4S LiPo battery (Zeee Power), which provided approximately 5-10 min of flight time with our full payload in approximately 2 m s −1 wind. The drone was piloted manually, using a 2.4 GHz controller (FrSky Taranis Q X7) that communicated flight commands to a compatible receiver (FrSky Taranis Receiver X8R) connected to the PixHawk.
Sensors. The PixHawk board has a built in IMU, which provides heading and attitude angles, angular velocities and linear acceleration measurements at 60 Hz. To provide ground truth data for our experiment, we mounted a 60 Hz GPS (Holybro M8N GPS) to the drone. Validating our estimation protocol required additional sensors for optic flow ( providing ψ) and air speed ( providing γ). For optic flow, we mounted a USB camera (ELP 1080p camera with a 2.9 mm wide angle lens) underneath the drone operating at 30 Hz. Image data were used to calculate two-dimensional optic flow in real time using the Lucas Kanade algorithm [32,33] with OpenCV [34]. For air speed measurements, we mounted a three-dimensional ultrasonic anemometer (Trisonica mini from Anemoment) to the top of the drone operating at 40 Hz.
Data collection. To collect sensor data, we installed an NVIDIA Jetson TX2 equipped with a a 1 TB NVMe M.2 SSD below the drone. We flashed the TX2 with Linux4Tegra and installed ROS (Robot Operating System) to provide a centralized interface to all of our sensors. We used the MavROS package to interface with the PixHawk, which provides access to the IMU measurements and RC controller inputs. Two other custom ROS packages were used to calculate optic flow and interface with the anemometer. All flight data were recorded into a single ROS bag file for offline analysis. All sensor data were linearly interpolated to the highest sensor frequency of 60 Hz prior to analysis, along with corrections for declination and sensor orientations.

Validation
To verify our algorithm's robustness to real-world data, we manually piloted a sensor-equipped drone in a designated radio controlled flight space (Reno Radio Control Club To provide a ground truth for the ambient wind direction estimates, we vector-subtracted the GPS provided absolute ground velocity from the two horizontal components of the apparent airspeed (figure 6e, blue). This estimate provides the best-case scenario for any two-dimensional wind direction estimator that relies on the airspeed measurements, allowing us to decouple the performance of the estimation algorithm and the choice of sensors. These GPS and airspeed derived estimates do deviate from the ground truth (median error of 21°). Some of these errors are explained by vertical wind measured by the dronemounted anemometer (linear regression R 2 = 0.1, p-val < 10 −3 ). Since the ambient wind exhibited very little movement in the vertical dimension, and the drone exhibited very little pitch and yaw during its trajectory, the changes in vertical wind measured on the drone were likely due to changes in downdraft from the rotors required for stabilizing the altitude of the drone. Smaller flying systems that introduce less downdraft to maintain lift (such as an insect) may be less impacted by this effect. Alternatively, future efforts will be needed to ensure a more accurate sensor estimates of airspeed direction.
Finally, we applied our optimal estimation algorithm to the sensor data to estimate the ambient wind direction with τ = 10 s (the approximate turning frequency of the drone), and two values of T ( figure 6e, red). The larger value of T = 50 provided better discrimination between the up-and downwind options. Our optimal estimation approach using only angular sensor measurements compares favourably with the ground truth estimate using the GPS and airspeed measurements (median error of 9.5°for T ¼ 50 s case). These results show that the algorithm outlined in §2 that relies purely on angular direction information is sufficiently robust to handle real-world data to provide wind direction estimates on par with other methods that also take advantage of magnitude information.

Results
Using a series of simulations, we set out to determine which types of trajectories can provide the most accurate ambient wind direction estimates. For each candidate trajectory, we consider (e.g. figure 7a,b), and for a variety of sensor noise magnitudes, we calculated the corresponding sensor measurements for either the dynamic or consistent wind scenario, and estimated the smoothed sensor estimates and their derivatives for a given choice of ω (e.g. using a cut-off frequency ω = turning frequency: figure 7c; a detailed analysis of ω is given in the following section). Then, for a given choice of τ and T, we estimated ambient wind direction values using our convex optimization framework (e.g. figure 7d) and calculated the associated errors (e.g. figure 7e). These errors largely stem from rapid changes in wind speed or direction, insufficient changes in course direction per unit τ, and sensor noise, as we will show in detail in subsequent analyses. Repeating this analysis for a variety turning frequencies and values of τ revealed that there is an optimal choice of τ for any given turning frequency (figure 7f ). In general, we find that choosing T = τ works well, however in some cases, particularly for small values of τ, choosing T > τ provides a modest reduction in the error compared to T = τ (figure 7f, stars), suggesting that distinguishing between upand downwind is a particular challenge (the following section will elaborate on the choice of T ). This is also apparent in the bimodal distribution of errors seen in some scenarios (e.g. turning frequency = 0.02 Hz, τ = 10, 50 s).
To better visualize the optimal choice of both τ and the turning frequency, we plot the median errors for each case as a heatmap (e.g. figure 7g). In many of the cases, we consider several combinations of τ and the turning frequency offer similar performance. To capture this range of optima, we determined which combinations result in a median error that is within ±5°of the optimum (figure 7g, red contour), and approximate this contour with an ellipse for visual simplicity. From this analysis, we can conclude that royalsocietypublishing.org/journal/rsif J. R. Soc. Interface 19: 20220258 for trajectories with 90°turns, variable velocity, and constant ψ = 0, in our dynamic wind scenario with sensors subject to modest noise, the optimal turning frequency ranges from 0.1 to 1 Hz, with values of τ ranging from 2 to 50 s.

Analysis of time constants
The optimal combinations of turning frequency and τ all occur where there is at least one turn per time τ (figure 8a). At sufficiently long values of τ, however, there is a high likelihood of the wind having changed direction in our dynamic wind scenario, which increases the median error. As a result, the optimal choice of τ is constrained to be large enough to capture a change in flight direction, but short enough to avoid likely changes in ambient wind direction. The changes in flight direction, meanwhile, must occur more frequently than changes in wind direction. In our dynamic wind scenario, the wind begins to show a high likelihood of changing direction after 10-50 s (figure 8a, blue dashed line), thus turning frequencies of 0.1 Hz or greater are required to achieve small errors.
To better understand the sensitivity of our results to choices of the cut-off frequency ω in our smoothing filter, we performed a parameter sweep of ω (figure 8b). We considered values of ω equal to the product of the turning frequency and five constants: [0.1, 0.5, 1, 2, 10]. For combinations of fast turning frequencies and short τ (upper left corner of the heatmap), choosing a smaller cut-off frequency (0.1*turning frequency) provided a modest reduction in median error of the wind direction estimates. Overall, however, a choice of ω = turning frequency generally performs well. This makes sense as the majority of changes in the sensory measurements are due to changes in course direction of the trajectory.
Next, we performed a parameter sweep of T. We considered values of T equal to the product of τ and four constants: [1,5,25,125]. For combinations of fast turning frequencies and short τ choosing T = τ performs poorly (figure 8a). However, choosing a much larger value of T, e.g. T = 125τ, substantially improves the accuracy of ambient wind direction estimates for these scenarios (figure 8c). For a turning frequency of 0.5 Hz and τ = 0.4 s, the optimal choice of T = 125τ = 50 s, which incorporates information across 50 individual turns. Overall, we conclude that assimilating sensory measurements across at least one turn is required, but is most important for distinguishing between up and downwind.

Analysis of turn angle and sensor noise
Next, we set out to answer the question: are small or large turns more effective for estimating the ambient wind direction under dynamic wind conditions? We repeated the analysis summarized in figure 8a for trajectories consisting of small, medium and large turns (figure 9a). Across a wide range of sensor noise, larger turns consistently provide more accurate ambient wind direction estimates. As with the first example provided, across all cases the lowest errors are achieved when there is at least one turn per unit τ. Choosing ω=turning frequency again works well across this entire range of turn angles and sensor noise levels (electronic supplementary material, figure S3). For short τ, estimation errors can again be reduced by choosing large enough values of T to incorporate measurements across at least one turn (electronic supplementary material, figure S4).
To provide some intuition about how accurate or poor the estimates of ambient wind direction can be, and characteristics of the errors, we show four representative examples in figure 9b-e. For frequent turns and short τ, the estimates are noisy, but the largest errors are characterized by errors in distinguishing between up-and downwind, i.e. errors of ±π (figure 9b). Small turn angles result in poor estimates even when information is assimilated across at least one turn (figure 9c). For large turns, assimilating measurement For larger values of sensor noise, regardless of the turn angle magnitude, the optimal range of turning frequency decreases slightly. One likely explanation for this is that as the sensor noise increases it becomes increasingly difficult to filter out noise without sacrificing the signal. Slower turning frequencies help to alleviate this by allowing the use of a more aggressive cut-off frequency that can smooth the noise more effectively. To maintain at least one turn per unit τ for these slower turning frequencies, however, means that the optimal value for τ must increase. This makes the estimator less able to respond to rapid fluctuations in wind direction (e.g. figure 9e).
There are essentially four categories of errors lumped together under our single error metric: (1) poor estimates of minðjẑ À zj + pÞ (i.e. the errors that are (much) less than π), which typically result from either too small a choice of τ or too small a turn angle (e.g.

Dynamic versus consistent wind
Next, we turn our attention to how the optimal values of τ, turning frequency, and turn angle change if the wind has a more consistent direction over time (electronic supplementary material, figure S5). In contrast to the dynamic wind case (where larger turn angles help to reduce the median error inẑ), we find that in consistent wind the overall error achieved by trajectories with small, medium and large turns is similar (figure 10), suggesting that large turns are particularly important when the wind direction is dynamic. Meanwhile, consistent wind is less sensitive to the specific choice of turning frequency or τ. That is, larger values of τ as well as slower turning frequencies are both effective.
To help summarize and generalize our results across a broader range of wind scenarios (figure 11), we individually analysed shorter segments from both the consistent and dynamic wind examples. We randomly selected 1000 segments of random lengths ranging from 1 to 1000 s. For each segment, we characterized the wind by the maximum and mean change in speed and direction relative to the middle of the segment, providing a measure of how self-correlated the wind was over that period of time. We then determined royalsocietypublishing.org/journal/rsif J. R. Soc. Interface 19: 20220258 the minimumẑ error for that particular segment, and the corresponding optimal τ's and turning frequencies associated with that minimum error ±5°(i.e. the location and extent of the red ellipses shown in figure 9). We find that errors inẑ are strongly correlated with the maximum change in both wind speed and direction (figure 11b). The optimal choice of τ decreases for more dynamic wind segments allowing the algorithm to respond more quickly, but as the sensor noise increases, larger values of τ are better (figure 11c). Trajectories with small turn angles fare better with larger values of τ, which allows the estimator to incorporate information across a larger time window. This does, however, result in increased errors in the dynamic wind cases. The optimal choice of turning frequency remains relatively constant across different wind dynamics, though slower turning frequencies can also be optimal for very consistent wind (figure 11d ). As sensor noise increases, the optimal turning frequency decreases, which makes sense since it becomes more challenging to distinguish between signal and noise.

Constant ground speed and constant airspeed direction trajectories
Finally, we turn our attention to two variations of the trajectory candidates to answer two questions: would trajectories with constant ground speed ( _ g ¼ 0) or constant airspeed direction (γ = 0) provide any improvements in the estimates over trajectories with variable ground speed and constant ψ = 0? These choices are motivated by the observation that many insects use visual information to maintain a constant ground speed [35], and typically orient their bodies in the direction of their airspeed [22]. The results for these trajectories are summarized in electronic supplementary material, figures S6-S8.
Constant ground speed trajectories show almost no change in optimal median error, τ, and turning frequency compared to the variable ground speed trajectories. Constant γ = 0 trajectories, however, do show one subtle but important improvement: smaller values of τ under high levels of sensor noise (electronic supplementary material, figure S6C). Why might this be the case? In our simulations, the wind speed was typically higher than the ground speed (mean wind speed was 2.3 m s −1 for the dynamic wind scenario; mean ground speed was 0.84 m s −1 for a turning frequency of 0.1 Hz). Thus, by adopting a constant γ = 0 trajectory the agents and their sensors are generally oriented in a similar  f remains quite small for the constant γ trajectories, making it easier to accurately estimate ϕ with large levels of sensor noise (electronic supplementary material, figure S9). With better estimates of the true sensor values, it is no surprise that the resulting ambient wind direction estimates are also more accurate.

Discussion
Recent release-and-recapture experiments with fruit flies in a feature-sparse desert showed that these tiny insects are able to locate the source of odour plumes from substantial distances over the course of approximately 10 min under a variety of wind conditions. In those experiments, flies were most successful at lower wind speeds [36]. Although this result is intuitive from a biomechanics perspective, these low wind speeds are typically associated with more variable wind directions [24], making the plume tracking task more challenging. Flies' plume tracking success under such conditions suggests that they are adept at following odour plumes in variable direction wind conditions. Our active anemosensing hypothesis provides a potential explanation for how they might accomplish this by integrating three angular sensor measurements known to be encoded in their central complex: orientation, airspeed direction and direction of movement.

Summary of key results
We begin our discussion with a summary of our key results (see figure 11; electronic supplementary material, figure S6).
(1) Large changes in course direction are most effective for accurately estimating the ambient wind direction. This is especially true when the wind direction changes frequently.
(2) For both dynamic and consistent wind cases, it is important that sensory information be integrated across at least 1 turn. (3) In more dynamic wind, shorter integration times (τ) are better, and faster turning frequencies are also necessary. (4) When subject to large amounts of sensor noise, slower turning frequencies offer better estimates by allowing the use of more aggressive smoothing filters to reject noise. (5) In terms of estimation accuracy, constant-ground-speed and constant-γ trajectories offer no substantial advantage over variable-ground-speed and constant-ψ trajectories. (6) For large amounts of sensor noise applied to trajectories with large turn angles, however, constant-γ trajectories allow for shorter integration times (smaller τ) compared to constant-ψ trajectories. (7) A key challenge is distinguishing between up-and downwind. In other words, it is challenging for an insect to determine if they are flying faster than the wind speed but headed downwind, versus flying slowly upwind. This challenge can be more reliably overcome by integrating information across a longer period of time (T > τ) to include information that spans more changes in course direction (figure 8).

Zigzag manoeuvres optimize wind direction estimation
The trajectories exhibited by flying and swimming animals during plume tracking across a wide range of taxa are nearly all characterized by some form of zigzagging, or casting [1]. Zigzagging, however, is not explicitly required for plume tracking if ambient wind direction information is directly accessible. For example, walking cockroaches are adept at following plumes but do not exhibit the classic zigzagging behaviour [37]. Walking Drosophila also do not need to perform zigzagging trajectories when presented with a sensor noise level sample trajectories median optimal error optimal turning frequency optimal t homogeneous and consistent odour plume, instead they will continue straight upwind [38]. For a walking animal, estimating the ambient wind direction from apparent wind estimates is trivial: they can either hold still for a brief moment, or rely on proprioceptive cues to accurately estimate their ground speed or distance travelled given their stepping speed and leg length [39,40]. Indeed, visually blind flies have no trouble in finding the direction of ambient wind [38]. Of course, some changes in direction are required in order to stay centred on the plume, especially for turbulent plumes [41], but these changes in direction need not be rhythmic, frequent nor large. Furthermore, in some efforts to reverse engineer plume tracking strategies using an 'optimal' reinforcement learning framework for agents that are provided with ambient wind direction information, the distinctive casting exhibited by animals appears to be largely absent [42], or diminished when the agent is inside the plume [43]. These findings together with our results presented here suggest that the zigzagging exhibited by flying insects, and likely other flying and swimming animals too, is crucial for them to estimate the ambient wind direction in order to guide plume tracking decisions.

Optimal choice of time constants
Our active anemosensing hypothesis involves the assimilation of sensory measurements across three different time constants: ω, τ and T. Our results suggest that a good rule of thumb for accurate ambient wind direction estimates is to choose ω = turning frequency, and τ = T ≥ 2/ω. This ensures that wind direction estimates are always based on information that spans at least one left or right turn. With noisier sensor measurements, slower turning frequencies and longer values of τ are helpful. The increased errors associated with shorter values of τ can be partially mitigated by using a much larger value for T.

Simplified mathematics with constant velocity control
It is unlikely that insects use the approach we outline in figure 3 due to the computational expense (solving the estimation for τ = T = 10 and dt = 0.1 takes ≈160 ± 20 ms on a powerful desktop computer). Here, we discuss some alternatives. Although plume tracking insects exhibit variability in their ground speed (e.g. figure 5e and [44]), it is also well established that they use visual feedback in order to try and maintain approximately constant ground speeds [35,36,45]. By assuming both a constant ground speed (g) and wind speed (w) over the period τ, their ratio (v = g/w) will also be constant. Assuming constant v it is possible to dramatically simplify the mathematics, as detailed in the following subsections. Future work will be required to determine how sensitive these approaches are to breaking their assumption of _ v ¼ _ g=w À _ wg=w 2 ¼ 0.

Quadrant sensing
The first simplification we consider does not aim to precisely determine the ambient wind direction, instead it only identifies which 90°quadrant the wind direction lies inside. This requires two comparisons of signs. First, recall the following equation (derived from equation (2.3) with δ = γ − ψ and α = γ + ϕ): v sinðdÞ ¼ sinða À zÞ: ð6:1Þ Although v is unknown, it is defined as a magnitude and must be >0, so we can write signðdÞ ¼ signða À zÞ: ð6:2Þ This relationship can be used to determine which hemisphere the ambient wind is coming from, and is the same constraint we use in our convex optimization formulation to disambiguate between up-and downwind. To further constrain the wind direction to one of four quadrants, we can place a similar constraint on the derivative of equation (6.1) (assuming _ z ¼ 0 over the period of time over which the derivative is calculated) We can now place the same type of sign constraint on this equation As long as j _ vj , jv _ dcotdj, we can ignore the _ v term such that we have the following constraint (which eliminates the unknown v from the equation): Together, equations (6.2) and (6.5) can be used to determine which of four quadrants the ambient wind direction is coming from. Note that although this approach does not provide a precise estimate of wind direction, it may be sufficient for some behaviours, and sequential quadrant estimates could be combined (e.g. through a Bayesian framework) to build a more precise wind direction estimate over time (i.e. over the course of several turns).

Considerations for neural implementation
A common theme across each of the simplified estimation approaches outlined above is the presence of a derivative (or finite difference) of sensory measurements. One plausible neural implementation for performing this calculation would be through a mechanism similar to the delay-and-correlate circuit employed for optic flow calculations [46,47]. Our analysis of different trajectories found that it is important for sensory information to be incorporated over a sufficiently large time constant, τ, such that information is assimilated from both sides of at least one turn. Thus, if insects do employ a delayand-correlate mechanism, we would expect the delay to be either synchronized with voluntary turns, or to be hardcoded as one or multiple values with a time constant equal to half of the typical turning period (i.e. 1 Hz based on figure 5). Each of the three simplified approaches require evaluating complex mathematical expressions containing trigonometric functions as well as multiplication and division. How might the brain perform these computations? In principle, a feedforward neural network can be used to approximate any of these mathematical expressions [48], however, accurate approximations may require too many elements to be practical. Instead, some simplifications or alternative reference frames may be used. The tangent function, for example, could be evaluated as a ratio of two orthogonal vectors, and recent work has revealed how flies can perform certain vector calculations related to navigation [16,49]. Multiplication and division in general can be converted to addition and subtraction if the calculations are performed in a log-space. Finally, it is quite possible that for the limited range of likely inputs an insect might actually experience due to environmental, biomechanical or behavioural constraints, the true mathematical expressions could potentially be simplified without introducing excessive errors.

Limitations
In our analysis, we only considered kinematically prescribed trajectories. Although flying insects do have exceptional gust rejection abilities [31], they will be thrown off course when subject to strong enough gusts. In such cases, it could be that estimation accuracy of wind direction actually improves due to the change in course direction, although only if the wind is not so overpowering that the insects end up drifting with the wind [23].
We focused our active anemosensing estimation framework exclusively on angular measurements known to be encoded in the central complex. A more traditional estimation approach from engineering would combine control inputs with these available measurements and a detailed dynamics model of the relevant physics to provide more accurate estimates (e.g. with a Kalman filter [50]). It is conceivable that insects could incorporate copies of their motor control commands with a model of the muscle output and aerodynamic forces to more accurately estimate the wind direction; however, this would require detailed calibrations of all of the model parameters. As with the sensory system, these parameters will vary in time due to damage [51], temperature fluctuations [52] or other circumstances, making it challenging to rely on a model-based approach. A significant advantage of relying exclusively on angular measurements is that the entire angular sensorimotor pathway relevant to ambient wind estimation can be self-calibrated without external help [23].

Hypotheses and predictions
If flies exclusively rely on visual anemotaxis, they would not be able to make arbitrary changes in course with respect to the ambient wind direction. Instead, they would need to determine upwind, or perform rhythmic manoeuvre centred around the upwind direction. Under our active anemosensing hypothesis, however, flies could continuously adjust their course angle without first orienting upwind.
With respect to computational complexity, the three estimation approaches that assume constant v = g/w provide the most biologically plausible implementations of our active anemosensing hypothesis. In each of these cases, we would expect to find a clear encoding of δ = γ − ψ in the flies' brain. Furthermore, we would expect to find neural pathways capable of differentiating this signal with a time constant roughly equal to half their stereotypical turning period of approximately 2 s.
We found that flies typically cast at approximately 0.5 Hz, i.e. approximately one left and one right turn every 2 s. Our active anemosensing hypothesis suggests that for this turning frequency, flies would have most accurate wind direction estimates for a τ ≥ 1 s. This would allow flies to be effective at tracking plumes in highly dynamic wind that changes direction on the scale of 1 s, possibly explaining the release and recapture successes at low wind speeds described previously. For insects with noisier sensory measurements, either due to fundamental sensor limits or environmental constraints such as low light levels, we predict that the turning frequency should decrease. Larger plume tracking animals such as albatross [53] could also conceivably employ active anemosensing to estimate ambient wind direction. With slower dynamics due to their more massive bodies we would also expect to see slower turning frequencies. For both cases, we predict that slower turning frequencies should lead to larger required values of τ and thus slower responses to rapid changes in wind direction.
One particular challenge our analysis highlights is that of distinguishing up-versus downwind. The most challenging scenario is for an insect to distinguish between flying downwind faster than the wind speed, versus flying upwind. To resolve this challenge, insects could use a similar approach to our solution (equation (2.15)), or perhaps they employ a combination of active anemosensing and visual anemotaxis to guarantee that during plume tracking behaviours they are always generally headed upwind, this would be equivalent to using a very large value of T.